Skip to content

-k mishandles numbers #7112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Sup3rGeo opened this issue Apr 23, 2020 · 2 comments · Fixed by #7122
Closed

-k mishandles numbers #7112

Sup3rGeo opened this issue Apr 23, 2020 · 2 comments · Fixed by #7122
Labels
topic: selection related to test selection from the command line type: bug problem that needs to be addressed

Comments

@Sup3rGeo
Copy link
Member

Using pytest 5.4.1.

It seems that pytest cannot handle keyword selection with numbers, like -k "1 or 2".

Considering the following tests:

def test_1():
    pass

def test_2():
    pass

def test_3():
    pass

Selecting with -k 2 works:

(venv) Victors-MacBook-Pro:keyword_number_bug fikrettiryaki$ pytest --collect-only -k 2
========================================================================================================== test session starts ===========================================================================================================
platform darwin -- Python 3.7.7, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
rootdir: /Users/fikrettiryaki/PycharmProjects/keyword_number_bug
collected 3 items / 2 deselected / 1 selected                                                                                                                                                                                            
<Module test_one.py>
  <Function test_2>

But selecting with -k "1 or 2" doesn't, as I get all tests:

(venv) Victors-MacBook-Pro:keyword_number_bug fikrettiryaki$ pytest --collect-only -k "1 or 2"
========================================================================================================== test session starts ===========================================================================================================
platform darwin -- Python 3.7.7, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
rootdir: /Users/fikrettiryaki/PycharmProjects/keyword_number_bug
collected 3 items                                                                                                                                                                                                                        
<Module test_one.py>
  <Function test_1>
  <Function test_2>
  <Function test_3>

If I make it a string though, using -k "_1 or _2", then it works again:

(venv) Victors-MacBook-Pro:keyword_number_bug fikrettiryaki$ pytest --collect-only -k "_1 or _2"
========================================================================================================== test session starts ===========================================================================================================
platform darwin -- Python 3.7.7, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
rootdir: /Users/fikrettiryaki/PycharmProjects/keyword_number_bug
collected 3 items / 1 deselected / 2 selected                                                                                                                                                                                            
<Module test_one.py>
  <Function test_1>
  <Function test_2>

I see there are some notes about selecting based on keywords here but it was not clear if it applied to this case:
http://doc.pytest.org/en/latest/example/markers.html#using-k-expr-to-select-tests-based-on-their-name

So, is this a bug? Thanks!

@bluetech
Copy link
Member

IMO this is a bug.

This happens before the -k expression is evaluated using eval, 1 and 2 are evaluated as numbers, and 1 or 2 is just evaluated to True which means all tests are included. On the other hand _1 is an identifier which only evaluates to True if matches the test name.

If you are interested on working on it, IMO it would be good to move away from eval in favor of parsing ourselves. See here for discussion: #6822 (comment)

@bluetech bluetech added topic: selection related to test selection from the command line type: bug problem that needs to be addressed labels Apr 24, 2020
@Sup3rGeo
Copy link
Member Author

@bluetech indeed! Thanks for the pointer, it seems to be really the same problem as the other issue.

bluetech added a commit to bluetech/pytest that referenced this issue Apr 25, 2020
Previously, the expressions given to the `-m` and `-k` options were
evaluated with `eval`. This causes a few issues:

- Python keywords cannot be used.

- Constants like numbers, None, True, False are not handled correctly.

- Various syntax like numeric operators and `X if Y else Z` is supported
  unintentionally.

- `eval()` is somewhat dangerous for arbitrary input.

- Can fail in many ways so requires `except Exception`.

The format we want to support is quite simple, so change to a custom
parser. This fixes the issues above, and gives us full control of the
format, so can be documented comprehensively and even be extended in the
future if we wish.

Fixes pytest-dev#1141.
Fixes pytest-dev#3573.
Fixes pytest-dev#5881.
Fixes pytest-dev#6822.
Fixes pytest-dev#7112.
bluetech added a commit to bluetech/pytest that referenced this issue Apr 25, 2020
Previously, the expressions given to the `-m` and `-k` options were
evaluated with `eval`. This causes a few issues:

- Python keywords cannot be used.

- Constants like numbers, None, True, False are not handled correctly.

- Various syntax like numeric operators and `X if Y else Z` is supported
  unintentionally.

- `eval()` is somewhat dangerous for arbitrary input.

- Can fail in many ways so requires `except Exception`.

The format we want to support is quite simple, so change to a custom
parser. This fixes the issues above, and gives us full control of the
format, so can be documented comprehensively and even be extended in the
future if we wish.

Fixes pytest-dev#1141.
Fixes pytest-dev#3573.
Fixes pytest-dev#5881.
Fixes pytest-dev#6822.
Fixes pytest-dev#7112.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: selection related to test selection from the command line type: bug problem that needs to be addressed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants