-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
PYI055 suggestion causes TypeError at runtime #6455
Labels
bug
Something isn't working
Comments
Thanks. |
(I think you mean |
charliermarsh
added a commit
that referenced
this issue
Aug 9, 2023
## Summary The use of `|` as a union operator is not always safe, if a type annotation is evaluated in a runtime context. For example, this code errors at runtime: ```python import httpretty import requests_mock item: type[requests_mock.Mocker | httpretty] = requests_mock.Mocker ``` However, it's fine in a `.pyi` file, with `__future__` annotations`, or if the annotation is in a non-evaluated context, like: ```python def func(): item: type[requests_mock.Mocker | httpretty] = requests_mock.Mocker ``` This PR modifies the rule to avoid enforcing in those invalid, runtime-evaluated contexts. Closes #6455.
Thank you for fixing this @charliermarsh . As it seems you can detect this case, perhaps it would be good to have this case be a |
durumu
pushed a commit
to durumu/ruff
that referenced
this issue
Aug 12, 2023
) ## Summary The use of `|` as a union operator is not always safe, if a type annotation is evaluated in a runtime context. For example, this code errors at runtime: ```python import httpretty import requests_mock item: type[requests_mock.Mocker | httpretty] = requests_mock.Mocker ``` However, it's fine in a `.pyi` file, with `__future__` annotations`, or if the annotation is in a non-evaluated context, like: ```python def func(): item: type[requests_mock.Mocker | httpretty] = requests_mock.Mocker ``` This PR modifies the rule to avoid enforcing in those invalid, runtime-evaluated contexts. Closes astral-sh#6455.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ruff 0.0.283 (error is new in this version)
Python 3.11.4
I get an error from ruff which proposes a solution, and applying that proposal causes an error.
I have the following file:
python example.py
has no outputruff --select=PYI --isolated example.py
shows:The ruff documentation at https://beta.ruff.rs/docs/rules/unnecessary-type-union/ suggests that this change is purely cosmetic:
However, if I make the change suggested, and then run
python example.py
, I get aTypeError
:The text was updated successfully, but these errors were encountered: