pytest icon indicating copy to clipboard operation
pytest copied to clipboard

Not recognizing paths with left square bracket

Open serjflint opened this issue 3 years ago • 14 comments

  • [x] a detailed description of the bug or problem you are having
  • [x] output of pip list from the virtual environment you are using
  • [x] pytest and operating system versions
  • [x] 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

serjflint avatar Oct 01 '22 11:10 serjflint

Have you tried putting the second command in " "

KingMichaelPark avatar Oct 01 '22 16:10 KingMichaelPark

Have you tried putting the second command in " "

I tried now and updated the question

serjflint avatar Oct 03 '22 14:10 serjflint

@KingMichaelPark here's the culprit: https://github.com/pytest-dev/pytest/pull/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 😄

nnrepos avatar Oct 14 '22 21:10 nnrepos

I think you should rename your directory 😉

KingMichaelPark avatar Oct 14 '22 21:10 KingMichaelPark

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?

nnrepos avatar Oct 15 '22 07:10 nnrepos

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

RonnyPfannschmidt avatar Oct 15 '22 07:10 RonnyPfannschmidt

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.

nnrepos avatar Oct 15 '22 08:10 nnrepos

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

RonnyPfannschmidt avatar Oct 15 '22 08:10 RonnyPfannschmidt

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)

KingMichaelPark avatar Oct 15 '22 09:10 KingMichaelPark

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 :)

nnrepos avatar Oct 15 '22 09:10 nnrepos

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 😅

nnrepos avatar Oct 15 '22 09:10 nnrepos

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

RonnyPfannschmidt avatar Oct 15 '22 12:10 RonnyPfannschmidt

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

asottile avatar Oct 15 '22 14:10 asottile

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. 🙂

MaggiR avatar Apr 24 '25 12:04 MaggiR