-
Notifications
You must be signed in to change notification settings - Fork 250
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
Mixing typing and non-typing information in annotations #482
Comments
Could you explain why you'd want to do something like this? This seems like something specific to your use case, so perhaps it would be better for you to implement this yourself? |
A simple way to achieve this without changing the way annotations work is to pass the runtime information with a decorator. |
I'd want something like this because I'd like to keep the benefits of type annotations (mypy verifying them etc.) and be able to add some extra information without much hassle. I am using a decorator-based approach @gvanrossum suggests, it looks like: @extra_info('param', 'xxx')
def function(param: Dict[str, int]) -> int:
... I have two issues with that - it puts the metadata in multiple places and requires one to repeat the name of the parameter - I was just wondering if mixing type and non-type annotations was ever discussed as that'd solve my problem. |
Yes, it was debated. There's a small section in PEP 484 under "Rejected Alternatives":
|
I also needed that in the past. To combine semantic types with structural types. In fact one could do that:
The issue is that mypy does not know how to type-check this. |
It's basically less readable than the solution with decorators. In the
future almost everyone will want to use types, but the kind of runtime
annotations you're using here are always going to have pretty limited
application.
|
If we had intersection types (#213) and protocols, you could include limited forms of extra information in annotations by declaring empty protocols that act like 'tags' that don't have any effect on static typing, but these could be used to encode information consumed by other tools. Mypy has a plugin system that has some limited support for changing the syntax of types, and you might be able to use it to hack something together -- but it would be mypy-specific. |
In some cases one can just use a |
Thanks, I'll keep an eye on the Intersection and protocol-related updates, this looks promising. |
First of all apologies if this is not the right project to post this question.
Sometimes one needs to annotate parameters and/or return values with not only types but some other information as well (an information that could be used at runtime).
The example below should show what I'm thinking about and what kind of API I have in mind:
I apologise if there was a discussion about something like this already, I haven't found any. What do you think about this particular need? There's no simple way to achieve anything similar right now, is there?
The text was updated successfully, but these errors were encountered: