-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Running multiple test cases across different files (crash via -k (twice)) #6822
Comments
I'm not entirely sure what you're trying to do (showing the contents of the files would help!) or what should happen, but if we're showing stack traces that's a pytest bug too. |
this is a known issue with keyword expressions - the are parsed as python expressions, its impossible to use |
I see. I case you are interested about a repro, these are my files
test_example_suite_3.py
|
(it should certainly not crash like that at least (twice)) |
I would like to work on this. Although this is my first time contributing to pytest, any guidance would be of great help. :) |
the key issue, that we run the mark expression unchecked against a predefined name-space that will only ever contain "bools" so operators and attribute access cant work, only name lookup and basic operators a solution would be to do a basic check on the string given in to detect unsupported operations in some manner |
I'd say a first step would be to have better error handling/reporting for such errors in a After that, I wonder if it's really worth continuing to work on the solution we have currently. At this point, it seems like it'd almost be easier to either hand-roll our own parser for those expressions or see what libraries there are we could use, rather than pretending something is Python code just so we can use |
hmmm, we could go the easy way and simply disallow subscriptions and attribute access, |
Hi @gdhameeja, Thanks for working on #6854! Unfortunately just checking for
(That's why also tests have failed). I think @The-Compiler has a point about us change to trying to parse the expression instead of calling
I second that, that should be simpler and a great improvement already. 👍 I think just catching pytest/src/_pytest/mark/legacy.py Lines 107 to 110 in acec0b6
Would be enough for a better message? |
I also agree we should parse manually and not use |
Unless anyone has a plan to do this without accidentally creating a breaking change, i would strongly recommend to just do basic checking /error handling Both mark and keyword expressions are a problem |
@bluetech I'd like to work on removing eval and parsing manually. But since I'm new to this idea, (and pytest) itself, I'd need some guidance. Can I please take this? |
I think compatibility should be preserved. Of course if it accidentally broken, then it should be treated as a bug an fixed. Although it depends on the details. |
Great! Here is how I would go about it: First I would read the current code to understand how it works. You can start with the This function calls into the What
Now, the idea is the get rid of the First I'd specify a formal grammar for the expression, which should determine exactly which expressions are well-formed (have proper syntax) and which are not. You can see for example Python's own grammar here, but note this one is much simpler, probably only a few lines to specify a Boolean expression with identifiers. You should be able to do it after studying the existing code. After you specify the syntax, you need to specify the semantics, i.e. how to determine whether a given well-formed expression evaluates to Then you can do the fun part which is implementing it. But before doing that, would probably wise to post a comment here with what you discovered and your plan. I hope I didn't make it sound too complicated. In the end, it shouldn't be. |
I would suggest to just parse to a python ast and blacklisting certain nodes |
Using Python's ast still has some disadvantages, for example this note from the docs:
Such notes should not be necessary I think... |
Going from a blacklist to ast construction is going to be simpler than inventing a new grammar Plus testing can be generalised |
I'm still -1 about pretending something is Python when it isn't, and then adding a pile of hacks on top of it to somehow shoehorn it into working (for some cases). Accidental breaking changes happen, that shouldn't prevent us from making things simpler. Certainly wouldn't be the first time we accidentally broke something in the process, but if that happens, we can always improve and cut a new release. I still think a grammar to support anything which currently is valid (and does something sensible) would be trivial. Maybe I'm wrong, but it'd nice to see some evidence that anything other than:
is really supported and used at the moment. It certainly isn't documented as being supported. |
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.
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.
Quick question, I am trying to run multiple test cases across different files using
-k
, but I am getting the following error:Are we doing this right? or is this a bug?
This is our enviroment
The text was updated successfully, but these errors were encountered: