-
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
FA100
and UP006
are technically incorrect and recommend code that can crash
#6617
Comments
If you're using a library that relies on evaluating types at runtime, I'd strongly recommend setting |
💡 Thanks, I wasn't aware of this setting! I see that this is filed as a "pyupgrade" setting... should this maybe be a top-level setting, as it seems to affect non-pyupgrade-related rules (e.g. |
This might be a question of preference, but to me it sounds like this should be the default behavior? I still think that these rules recommend invalid code, even if the resulting error is suppressed. |
It's definitely a reasonable opinion but I likely won't change the default for now. I left it as non-default to match pyupgrade. There are a few discussions to that effect in the pyupgrade repo (for example, asottile/pyupgrade#587). The gist of it is that code relying on type annotations at runtime is less common than code that does not. You could imagine that making this default would also be confusing to some users ("I have Personally, I'd probably recommend against using |
I understand that there are many discussions in the Python community about Still, But that's my last attempt at convincing you, in the end it is your call! As long as I can disable this for my project (with |
PEP 563 enables using |
Mypy allows |
I appreciate all the additional context you've provided! It'll be helpful guiding our decisions in this area in the future. For now, it looks like there is consensus in tooling that using generics with standard container types is desirable to support in older Python versions and changing our defaults would be a breaking change that would be hard for our typical user to understand. Let us know if you run into any troubles with |
Hi everyone,
(adding @TylerYep who added
FA100
to ruff, not sure who would be responsible forUP006
)thanks for everyone's efforts in creating and maintaining ruff!
While trying to enable all ruff rules, I came across an issue with rule
FA100
. I wasn't sure if I should report this in the repo for the corresponding flake8 plugin or here. But since I don't use flake8, but only ruff, I decided to create the issue here.The rule states:
However, this is technically not correct. PEP 563 only causes postponed evaluation of type annotations. It does not allow using generic types on standard collections (e.g.
list[str]
), which is covered by PEP 585. Enabling the postponed evaluation of type annotations only means that the errors of using generic types on standard collections will actually not be raised until you evaluate the type. I admit this is a bit confusing. I have just recently asked about this on the CPython repository: python/cpython#107530So using
from __future__ import annotations
will just suppress the errors of using those incorrect types as long as you don't evaluate them. I just run into an issue with Pydantic, which will evaluate the type annotations to generate appropriate field validators. So the following code evaluates fine on Python 3.8:But ruff will show me a warning for FA100:
However, if I follow this suggestion I will end up with:
Where
list[str]
is technically not a valid type annotation in Python 3.8, but the future import will suppress this issue by not immediately evaluating the type annotation. But when I execute this code in Python 3.8, it will throw an error, because Pydantic will evaluate the annotation (rightfully so):I think there's a corresponding issue with UP006, which will flag the following code:
and recommends to replace
List
withlist
. But this will similarly create invalid code that will throw an error at runtime.The text was updated successfully, but these errors were encountered: