Skip to content

Not recognizing paths with left square bracket #10329

Open

Description

@serjflint
  • a detailed description of the bug or problem you are having
  • output of pip list from the virtual environment you are using
  • pytest and operating system versions
  • minimal example if possible
pip list
Package    Version
---------- -------
attrs      22.1.0
iniconfig  1.1.1
packaging  21.3
pip        22.2.2
pluggy     1.0.0
py         1.11.0
pyparsing  3.0.9
pytest     7.1.3
setuptools 65.4.1
tomli      2.0.1
wheel      0.37.1

pytest skips everything after a left square bracket if provided with a path to a folder

~/workspace/pytest_test via 🐍 v3.10.6 (pytest_test) 
❯ pytest
============================= test session starts ==============================
platform linux -- Python 3.10.6, pytest-7.1.3, pluggy-1.0.0
rootdir: /home/user/pytest_test
collected 2 items                                                              

simple/d1/test_public.py .                                               [ 50%]
simple[hard]/d2/test_public.py .                                         [100%]

============================== 2 passed in 0.01s ===============================
~/workspace/pytest_test via 🐍 v3.10.6 (pytest_test) 
❯ pytest simple\[hard\]/
============================= test session starts ==============================
platform linux -- Python 3.10.6, pytest-7.1.3, pluggy-1.0.0
rootdir: /home/user/pytest_test
collected 1 item                                                               

simple/d1/test_public.py .                                               [100%]

============================== 1 passed in 0.00s ===============================
~/workspace/pytest_test via 🐍 v3.10.6 (pytest_test) 
❯ pytest 'simple[hard]/'
============================= test session starts ==============================
platform linux -- Python 3.10.6, pytest-7.1.3, pluggy-1.0.0
rootdir: /home/user/pytest_test
collected 1 item                                                               

simple/d1/test_public.py .                                               [100%]

============================== 1 passed in 0.01s ===============================
~/workspace/pytest_test via 🐍 v3.10.6 (pytest_test) 
❯ pytest "simple[hard]/"
============================= test session starts ==============================
platform linux -- Python 3.10.6, pytest-7.1.3, pluggy-1.0.0
rootdir: /home/user/pytest_test
collected 1 item                                                               

simple/d1/test_public.py .                                               [100%]

============================== 1 passed in 0.01s ===============================

Check out the provided path in the second block and the path pytest tests

Activity

  1. KingMichaelPark commented on Oct 1, 2022

    @KingMichaelPark

    Have you tried putting the second command in " "

  2. added
    topic: collection
    on Oct 3, 2022
  3. serjflint commented on Oct 3, 2022

    @serjflint
    Author

    Have you tried putting the second command in " "

    I tried now and updated the question

  4. nnrepos commented on Oct 14, 2022

    @nnrepos
    Contributor

    @KingMichaelPark
    here's the culprit: #9642
    it seems like square brackets are considered part of pytest's syntax.
    not sure if this is a bug or lack of documentation/warning then.
    if you're interested in a fix, please let me know 😄

  5. KingMichaelPark commented on Oct 14, 2022

    @KingMichaelPark

    I think you should rename your directory 😉

  6. nnrepos commented on Oct 15, 2022

    @nnrepos
    Contributor

    I think you should rename your directory 😉

    this is clearly not a sufficient solution. what about projects which use brackets in hundreds, thousands of directories?
    something like this should, at the very least:

    • be well-documented
    • raise a warning

    @nicoddemus, could you share your opinion please?

  7. RonnyPfannschmidt commented on Oct 15, 2022

    @RonnyPfannschmidt
    Member

    Brackets are not supported as part of python module names, so unless extra contex is provided i consider it a no go

  8. nnrepos commented on Oct 15, 2022

    @nnrepos
    Contributor

    Brackets are not supported as part of python module names, so unless extra contex is provided i consider it a no go

    according to the docs:

    If no arguments are specified then collection starts from testpaths (if configured) or the current directory.
    Alternatively, command line arguments can be used in any combination of directories, file names or node ids.
    

    this paragraph implies that pytest also supports test directories, not just test modules.

    regardless of that, shouldn't the code be able to identify and warn about these "illegal" directory names?
    i think it would help users understand what is going on, and rename their directories.

  9. RonnyPfannschmidt commented on Oct 15, 2022

    @RonnyPfannschmidt
    Member

    Some type of warning seems sensible

    As far as I can tell in the last 15 years this is the first time that brackets in folder names came up as a request

  10. KingMichaelPark commented on Oct 15, 2022

    @KingMichaelPark

    Yeah I have never seen them, what's the use case for hundreds of thousands of directories with brackets in them (completely out of curiosity not a criticism)

  11. nnrepos commented on Oct 15, 2022

    @nnrepos
    Contributor

    Some type of warning seems sensible

    As far as I can tell in the last 15 years this is the first time that brackets in folder names came up as a request

    that's probably because the PR that broke them was pushed this february, and not everyone is using the latest pytest version.
    if you would like me to add a warning in a new PR, please let me know.
    thanks :)

  12. nnrepos commented on Oct 15, 2022

    @nnrepos
    Contributor

    Yeah I have never seen them, what's the use case for hundreds of thousands of directories with brackets in them (completely out of curiosity not a criticism)

    i honestly don't know, i just know it's possible 😅

  13. RonnyPfannschmidt commented on Oct 15, 2022

    @RonnyPfannschmidt
    Member

    Oh yay, @asottile seems like a opportunity for pain was missed, seems like a preliminary look at the Filesystem is necessary after all 😭

  14. asottile commented on Oct 15, 2022

    @asottile
    Member

    I'd rather just say "play stupid games win stupid prizes" -- putting square brackets in directory names imo clearly falls into that category

  15. MaggiR commented on Apr 24, 2025

    @MaggiR

    Hi there!

    I also just ran into that issue on my Windows machine. It took me 3 hours to determine that the root cause comes from pytest's inability to handle brackets in the path. I am using PyCharm, which passes absolute (full) paths to pytest as arguments. As a consequence, if any of the test root's parent directories (even outside the project) have "[" or "]" in their name, pytest breaks.

    A simple warning would be very helpful here. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: collection

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions