Skip to content
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

Ruff unconditionally assumes any string in a type parameter is a forward annotation #11378

Open
lanzz opened this issue May 12, 2024 · 1 comment
Labels
type-inference Requires more advanced type inference.

Comments

@lanzz
Copy link

lanzz commented May 12, 2024

I have an alias for Annotated, to be used in a container class:

class Container:
    # class that inspects its type annotations at runtime and does stuff with them

Field = typing.Annotated

class MyContainer(Container):
    foo_field: Field[int, 'metadata']    # ruff raises F281 "Undefined name `metadata` here
    bar_field: Field[str, 'not a valid identifier']   # ruff raises F722 "Syntax error in forward annotation" here
    foo_ann: typing.Annotated[int, 'metadata']    # no complaints here
    bar_ann: typing.Annotated[str, 'not a valid identifier']   # no complaints here either

Ultimately, the Field alias is just sugar to make the definitions of subclasses of Container better self-documenting. I understand this is a pretty niche use and it's very likely not feasible for Ruff to figure out that Field is alias for Annotated (especially since in practice that alias definition is in a completely different module), but is there a way to explicitly instruct Ruff to treat those instances of Field the same way it treats actual Annotated type definitions, without having to suppress the rule explicitly on every single line that uses Field? If I could define ignores by line regex that would work (e.g. ignore F281, F722 for lines matching :\s+Field\[), but I don't think Ruff supports that, or at least I could not find it in the docs.

@charliermarsh
Copy link
Member

I don't know that we have a great workaround here right now. If we can't resolve a symbol, and it's subscripted in an annotation, we do assume that any strings within the subscript are forward annotations, since they're almost always generics.

I suspect this will eventually "just work" as we're doing a lot of foundational work right now to make Ruff capable of this kind of inference. But in the interim, struggling to think of anything other than adding # ruff: noqa: F281, F722 to the file.

@charliermarsh charliermarsh added the type-inference Requires more advanced type inference. label May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-inference Requires more advanced type inference.
Projects
None yet
Development

No branches or pull requests

2 participants