-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
UP007 problems with pydantic and from __future__ import annotations
#5434
Comments
Yeah, the problem stems from the fact that pydantic evaluates the type annotations at runtime. I've had similar problems with hydra which also evaluates type annotations in order to do type validation at runtime. Also note that Ruff could check for these libraries and exempt them from UP007 but that would be a lot of special casing because I don't think you can in general tell whether a type annotation will be evaluated at runtime without knowing what the library does internally. |
While it's true ruff can special case imports of pydantic and just assume all annotations aren't safe to apply UP007 to, it's also true that UP007 is quite narrowly scoped and should arguably be disabled entirely in codebases that both support older versions of python, and use pydantic in some form. A dedicated config setting for "i-use-runtime-annotations-please-dont-break-that = true" would allow UP007 to smartly detect when the minimum required python is updated and automatically re-enable itself. |
Yeah, I generally recommend against using We actually used to have a |
I'm going to use this Issue as an opportunity to reintroduce that |
My suggestion would be to actually reverse the flag, so that people don't break things by accident. |
Consider the following file, written for Python3.9+.
Running
ruff check tmp.py --target-version py39 --select UP --fix
with Ruff version0.0.275
yields:Running the script results in the following error:
The module-level x seems to work fine (because of the
__future__
import?). Should Ruff maybe not change the type annotation iffrom __future__ import annotations
is present? The type annotation might be evaluated in a different context where the import is not available.The text was updated successfully, but these errors were encountered: