-
Notifications
You must be signed in to change notification settings - Fork 258
Dealing with modules that don't expose type objects #23
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
Comments
My vote is for option 4. If the type object is still unavailable, falling back to Any is sadly the only option. Could we somehow use a decorator in a stub to inform the type checker that a class/function is a stub for a different name? This looks like it's blocked on a decision what to do with protocols. @gvanrossum? |
More discussion here: https://github.com/JukkaL/typing/issues/18 |
So we're not doing protocols this time around. Let's add a typing.re submodule that defines Match and Pattern, and also let's add a typing.io submodule for IO and its subclasses. Separately, we should add re.Match and re.Pattern in 3.5 to the regular stdlib re module. But if you want to use them as generic types (e.g. |
At least the following modules could also benefit from having
There are most likely others that don't have any stubs yet. Maybe we should go through the standard library (or at least the most commonly used modules) and try to find additional examples of generic classes. Once the set of |
At least for the duration of 3.5, this will be in provisional mode which means we can still make changes. (We do this for asyncio in 3.4 and it's great -- and nobody has complained so far.) Also, adding new submodules (that have to be explicitly imported to be used) sounds like a fine thing to do at any point in the future. At some point it may be better to make the implementations themselves generic, so people can start using e.g. One subtlety is, do we require |
Fixed in e2e6fc4. |
Some modules (for example,
re
) don't expose some of the underlying type objects, which makes it tricky to annotate code that uses these types. Mypy defines custom type aliases intyping
for some of the more common types (including pattern and match object types forre
), but this does not easily extend to 3rd party libraries.Example:
A related issue is types that are implicit, such as protocol types / structural types (in case we support them). Where should a structural type needed for annotating a library module be defined?
Also, generic library types pose a similar problem, since vanilla Python classes don't support indexing, and we'd have to use the string escape syntax to use a library class as a generic type.
Here are alternatives I've came up with:
Any
if the type object is not available. This is clearly suboptimal.typing
or a related module as an alias. This does not scale to an arbitrary number of modules, but it would probably work for the most common cases. We could fall back toAny
elsewhere.typing.re
forre
. Not sure how this will work if these modules come from multiple sources. Should this hierarchy be managed centrally?_sre.SRE_Pattern
in the above example.The text was updated successfully, but these errors were encountered: