-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Discuss: A module for experimental extensions to the "typing" module #2210
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
In #2206 where I proposed the idea of a
Therefore I propose adding a The preceding is one use case for extending the @gvanrossum indicates that there is a preexisting |
Thanks for opening a new issue! I'd also like to include by reference my comment from #2206, at least the part where I explained that the name of the new module should not be "mypy dot something" because of the meaning of the mypy package. I'm open for another name that has "mypy" in it, e.g. "mypy_extensions.py" -- I just don't think it should live in the mypy package. We should also consider whether the new module should act as an extended copy of the typing module, or whether it only exports new extensions. IOW could we write "from mypy_extensions import Any, List, TypedDict" or would we have to write "from mypy import Any, List; from mypy_extensions import TypedDict". Each has some pros and cons, as you can imagine. |
Indeed I note the comment that the mypy typechecker (and hence the
I'd be in favor of a module that only has new extensions. Reasoning:
By analogy, imagine if a website used 100% vendored Another interesting question is whether it is only the
Since the future is uncertain, I'd lean toward the more future-proof One might quibble over the number of letters in |
You've pretty much convinced me that the proposed module should only contain extensions. I've got some small worry left about a possible situation where an extension requires modification of an existing type (say, if we wanted to add some new flag to I'm also okay with naming the new thing |
Definitely more convenient. It would imply that mypy_extensions would be version-locked with mypy. I don't think this restriction is a problem because changes to mypy_extensions would be tightly coupled with mypy anyway. Having mypy's setup.py install mypy_extensions automatically is an interesting idea I hadn't considered.
|
Currently "pip install mypy-lang" also installs types.py for the current Python 3 version, if that's < 3.5. So for Python 2 or if your Python 3 target is different than your the Python 3 version you use to run mypy, you still have to do "pip install types". We could make it so that "pip install mypy-lang" also installs mypy_extensions, but you could also run a separate "pip install mypy_extensions". All this could live in the mypy repo; the version-lock doesn't bother me (we should make sure that the separate install has a matching version). |
Okay I like this approach. What would the directory structure look like? Maybe something like:
This structure has the advantage of giving the mypy_extensions setup.py a nice home. But I suspect that the inability to import |
|
Maybe just "extensions" for the top-level?
Stubs? Do you mean mock objects of some kind?
I don't think I follow. If mypy_extensions/typing.py has inline type annotations, why would there need to be an external .pyi file? |
Ah, you haven't truly grokked the wonderful world of mypy unittests. For speed reasons they have very small custom stubs that are substituted for builtins.pyi by the test framework. That's why many tests have a Regarding the need for a separate (real) typing.pyi, the reason is that file is full of hacks that mypy wouldn't like, so we don't type-check typing.py (same reason why mypy doesn't analyze the stdlib by default, and it's not a good idea to change that). |
Cool, this could be very useful for other experimental features as well. Naming the top-level directory By the way, PEP 8 discourages underscores in package names -- but is something like |
I would disregard PEP 8 here. That particular rule is probably really only valid for the stdlib. |
Okay it sounds like we do need a typing.pyi. However could we put this typing.pyi adjacent to mypy_extensions/typing.py rather than having it in typeshed? Typeshed feels like the place to put .pyis when the original library is not willing to provide its own annotations (either inline or via .pyi). In the case of mypy_extensions I think it would be easier for it to manage its own .pyi instead of externalizing it. Thoughts? |
I think that it's okay to put it in typeshed, at least for now, because that's currently the only standard location from where other tools can find stubs. If a library has annotations, there's no good way to declare this fact, and users will likely have to manually configure their tool to look at the library implementation for annotations. (Of course mypy can special case the stubs for its internal stuff so that it will always find them.) |
If I understand correctly, by default typing annotations are looked for (1) inline and (2) in a .pyi that is specifically in the typeshed project? But not (3) in a .pyi file that is alongside the original .py file or otherwise on the PYTHONPATH? |
No, it depends on the search path (MYPYPATH, and augmented by the
directories containing the modules passed on the command line). If the
search path finds both a .py and a .pyi in the same directory it will
prefer the .pyi file.
|
This issue tracks a discussion originating in #2206 about creating a module for experimental extensions to the standard "typing" module.
The text was updated successfully, but these errors were encountered: