-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Improving support for Annotated[Type, ...] annotations #12619
Comments
not totally related but I would at least like typing tools to indicate the presence of an annotated type when using functions like from typing import Annotated
MyInt = Annotated[int, "myint"]
x: int = 5
y: MyInt = 10
reveal_type(x)
reveal_type(y)
|
@Zzeek, what do you mean by "the presence of an annotated type"? Are you talking about the declared type of a symbol? Keep in mind that |
I would like a function that returns a particular type, such as: def my_function() -> MyType:
... to use that return type when revealed: x = my_function()
reveal_type(x) # would show MyType The above behavior is obviously the usual case, if the docs for Annotated state that this is basically the correct default behavior, which is fine:
However I am proposing that there be "special logic" that affects how the typing tools display the type in their UX, such that it can be differentiated from the non- |
Ah, I misunderstood what you meant by "annotated type". I now understand. You're talking specifically about the use of The challenge with Another challenge is that types can be combined (in unions) and transformed (narrowed, specialized, etc.). There are well-defined rules in type theory for performing these transforms. It's not clear how additional metadata specified by |
right, in my case, just showing the name of the type, rather than the type it's representing, would be enough. but i understand that's a bit arbitrary. |
Perhaps asking for the same thing - what I'd like to see is a plug-in interface explicitly targeting Searching for how to deal with |
I think part of the problem with defining a plugin interface for this is that |
Hmmm.... my naive understanding is that only point of type annotations was to implement a single function In this view, if we'd like to allow for "any" plug-in, then "all" we need to do is to add a list of The implementation of Assuming that the That said, I have no idea on how |
We encountered the same issue in #16094. Maybe a good first step would be to start type checking the metadata expressions. |
Also encountered this and just checked against pyright. Pyright detects typing issues in annotated value expressions as "expected" (at least expected by me). IMO since all following arguments after the first one to |
When writing plugins for code which uses
Annotated[Type, an_obj, ..., another_obj]
types dynamically, it would be nice to be able to access these annotations in order to either (a) perform type-checking on them, and/or (b) use those dynamic annotations to correctly infer the existence or types of other objects. I'm a total newbie to the mypy internals, but I think this is not currently possible. (See also issue #10872, and PRs #9625, #10777.) It would be fantastic if mypy could support the proper treatment of later parameters ofAnnotated
somehow.If I understand the current situation correctly, by the time the plugin gets a hold of the type annotation of something which was
Annotated
, even theunanalyzed_type
, mypy will have run theTypeConverter
on all of the parameters of theAnnotated[...]
annotation. This unfortunately erases most of the information in them, rendering them useless to the plugin. Note that PEP 593 statesso it is in principle wrong to treat all of the parameters of
Annotated[...]
as types anyway.However, I presume the parser cannot easily tell whether a given
Subscript
node in the AST is anAnnotated[...]
field in the relevant part of fastparse.py where this type conversion happens. That is, it cannot really distinguish betweendict[str, "Something"]
andAnnotated[str, "Something"]
in order to treat these cases differently (i.e. the first refers to a typeSomething
which happens to be in a string, and the second simply contains a string"Something"
).I'm not familiar enough with mypy internals to know what the best solution is. The fundamental tension that mypy wants to do type conversion directly on the AST, but it's not until semantic analysis starts that it knows which nodes are Annotated. I can see that during semantic analysis,
expr_to_unanalyzed_type
is used. Given that this is possible, it seems to me like the most natural thing is if the AST parsing stage does not do type conversion onSubscript
parameters, but instead converts them toExpression
s which are later converted to types if appropriate. Presumably this comes at some performance cost, though, in the more common cases of parameters being types (dict[str, int]
orlist[str]
etc.).Any thoughts would be fantastic.
The text was updated successfully, but these errors were encountered: