-
Notifications
You must be signed in to change notification settings - Fork 258
Classes available during type checking only #597
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
How do you propose to mangle the name in a decorator at runtime? The decorator has no control over the name to which the result is assigned. |
Actually mangling the name is probably not the best option. Having the name importable at runtime makes sense so it can be used in type annotations without |
An alternative way to do this would be that the decorator is only available in stubs. Type checkers would be expected to produce an error for any use of a class decorated with I don't see much use for using this decorator at runtime; if you're exporting the class anyway, you might as well make it visible. Therefore, people using such classes would still have to import them within |
I don't have a really convincing argument why the decorator needs to be available at runtime. The quite weak arguments I have are:
These are the reasons that make me believe (although not very strongly) that having it available at runtime makes some sense. Another alternative runtime implementation is to immediately raise an error if |
|
Based on this discussion, I would suggest to do the following:
If it turns out we need a runtime version, we will add it then. |
Let's say we are creating a set of type stubs for a library that we do not control. if TYPE_CHECKING:
from library import behavior_class
my_var : behavior_class My example arises from a library that offers a number of data structures that all support a particular kind of lookup behavior, but that are not all of the same class. I do not control this library, so I can't push |
@rpgoldman This is exactly my use case. You can quote the type or use Edit: All of that is already possible to do in type stubs. This decorator would be more or less a standardized no-op to signal types that are not available at runtime. |
@srittau Thanks! The use of quoting in type hints had slipped my mind! Sorry! |
So it looks like the latest design here has the decorator only available in stubs. Why isn’t a comment (maybe standardized) just as good? |
@srittau Is there still a desire to do this? Or should I just close all the PRs without merging? |
Sorry, yes, I still think this has merit. But at this point, I'm unsure on how to proceed. I see several options, but there has been resistance to each of them:
Personally, I favor 4 slightly over 3, but the discussion here seems to favor 3. But ultimately, someone has to make the decision with which option to proceed. |
I'm actually okay with either 3 or 4, and I leave it up to you to choose. 3
is easier (since you don't have to push updates to the typing_extensions
and typing packages -- only to typeshed and the CPython docs).
|
Let's do 3 for now. 4 can always be done later. |
SGTM.
|
Per discussion in python/typing#597.
The three commits implementing (1) have all been merged, so closing this now. Thanks! |
Per discussion in python/typing#597.
This came up in srittau/peps#21: Stubs may need to define classes that are not available at runtime. Best examples are the return types of
re.compile()
andre.match()
, although those are handled specially intyping
.One solution we came up with is to add a class decorator to
typing
, for example@type_check_only
. While type checking this is a no-op, during runtime it mangles the name of the class, so that it is not available in the module namespace under its original name.This is mostly intended for type stubs, but could be used in implementations (starting in Python 3.8). We would add it to typeshed without a version check, so it can be used in stubs immediately. Alternatively, type checkers could special case the import. The documentation should also recommend to make the real class available in own code, instead of using the decorator,
@rchen152 @Solumin
The text was updated successfully, but these errors were encountered: