-
Notifications
You must be signed in to change notification settings - Fork 235
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
Support interoperability with other annotations and type systems #44
Conversation
Support interoperability with other annotations and type systems by requiring separate namespaces.
I don't like the tuple notation. I see several options that you might pursue:
Also, it would be more useful to open an issue for discussion before proposing a textual change (unless the change in non-controversial, which you know this isn't). And, "typing.integer" isn't a thing. |
I thought it was ok to propose a text change as a basis for discussion. Pull requests are quite cheap and they're essentially just issues with changes. I actually think that tuples are ok as long as their content is unambiguous, which is what I'm trying to assure with the namespace requirement. Lists (i.e. square brackets) might look a little nicer syntactically, but wouldn't catch the idea of different kinds of annotations as much as tuples do. The alternative would be dicts, but they are very verbose and that verbosity is not needed if each item in the tuple is semantically well defined by its namespaced type. I think tuples are as simple and straight forward as it can get. Are you suggesting that interoperability isn't even worth the overhead of a tuple? What's your opinion on an extensible type system? Don't you think it'll be necessary? |
BTW, Steven D'Aprano wrote something pretty compelling on python-ideas. Maybe we can use some of his language in the PEP: """ There is no good way to have multiple uses of annotations be used in the Besides, the more information you try to squeeze into the function Type-hinting was Guido's original motivation for introducing
If some other use of annotations becomes wildly successful, then it If you want to interoperate with type-hinting, decorators may be a |
The problem with a PR is that it obscures the essence of the proposal with a lot of editing details. That may make sense for code (where trying to explain it in English is often more verbose than showing the code) but not for text (where the diff is significantly more verbose than just the proposal, clearly worded). Also, a PR doesn't show the text in the same "stream" window as the rest of the discussion.
Well, I really don't like them. And I want type hints to become the primary use of annotations.
I'm not sure what you mean by an extensible type system. Clearly you're not referring to the ability to define new types. But then what? New constructs like Intersection (#18) or Protocols (#11)? Those can be discussed and added in a future version; it's hard to see how you could add those without changes to the type checker, and an "escape" notation like you propose isn't going to help. And sticking documentation in an annotation (like your |
I'm one of the users of custom annotations (to export functions to the user as "commands" they can invoke in my application), and I've tried to follow the discussions so far.
How would that work? I'd imagine something like a decorator I don't like the idea of being unable to use the type checker for functions with custom annotations, i.e. only being able to skip things via a decorator. I'd prefer to make them work using a decorator. |
The problem with your proposal is that it's not enough to specify how you would get the type annotation for a parameter -- you must specify it in a way that the static type checker can use. The static type checker reads your code but doesn't ever import it. IMO it's unreasonable to expect the type checker to understand I'm not sure what Steven D'Aprano meant with "Foo + type-hints" but I don't see a reasonable interpretation except that it refers to you writing your own type checker, which you can make behave however you want to. |
That is a good point. Then I'd still be in favour of a dict-syntax being supported optionally. That'd work for me (I currently use dicts), but indeed, I'm not sure how useful that'd be universally. An example of how I use the annotations: @cmdutils.register(...)
def scroll_perc(self, perc: {'type': float}=None,
horizontal: {'flag': 'x'}=False,
count: {'special': 'count'}=None): This is indeed rather heavy to read, but very convenient to me. This would expose a command with a |
But are you really interested in running mypy over your code and over the application code that is calling your commands? How does an application even call those commands? How doe the non-type-hint annotations come into play? If an application does a dynamic lookup of commands in your library and then calls them, the type checker isn't useful anyway -- the type checker only works for (shall we say) "static" calls where the name of the called function (or method) is in the caller's code. Example:
can easily be type-checked (assuming there are annotations on
Now, if your use case looks more like the former, type annotations on whatever are useful. You can do two things: either add type annotations directly in the source code for somewhere/something.py, or you can create a stub file that is seen by the static checker but not at run time. If you use a stub file, you can still use annotations for your own nefarious (:-) purposes, but you must disable type checking own code (i.e. something.py) using a per-method or per-class decorator or a "# type: OFF" comment. Only the application will be type-checked (and only if the app developer runs mypy). If you want your own code to be checked by mypy too (or primarily), you will have to give up on using annotations for your own flags -- you're going to have to invent some decorator. (Because I still don't believe that the ugliness of supporting your dict proposal is worth it.) |
Indeed, I just realized the same while catching up with the thread on python-ideas. Those functions are indeed called dynamically, so it'd probably suffice to have a "don't check this" decorator. Am I right when I assume the decorator has to be an actual decorator above the function so it can be inferred statically - i.e. I couldn't apply the decarator in my |
Yeah, see #51. |
Sorry, should have closed this back in January. |
Support interoperability with other annotations and type systems by requiring separate namespaces.