-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Runtime use of type_check_only classes should give an error #9531
Comments
Can you provide an example? Do you have a specific package in mind? |
Sure. The specific package that prompted me to suggest this is https://github.com/googleapis/google-api-python-client, which I've been working on generating a stub package for. There are two different aspects of this library where this would be useful: Dynamically generated classesAll API resources represented by this library are instances of the Resource class that have had methods dynamically created and attached at runtime based on an API schema. To be able to annotate this I generate "fake" subclasses of You can see some examples of the classes I've had to generate here: https://github.com/henribru/google-api-python-client-stubs/blob/master/googleapiclient-stubs/_apis/sheets/v4/resources.pyi Another case I know of where this is relevant is https://github.com/boto/boto3. It doesn't stick everything onto instances of a single class, but dynamically creates subclasses, which leads to the same kinds of annotations. This stub package looks like it has the same issue as mine of having to include stub-only classes. I'm sure there are many other examples too, but it might be especially prevalent in these kinds of API wrappers. Object-like dictsThis is probably the more common case. Lots and lots of libraries use dicts as a sort of quasi-object for argument and return types. When creating stubs for these it's really helpful to include You can see examples here of this case: https://github.com/henribru/google-api-python-client-stubs/blob/master/googleapiclient-stubs/_apis/sheets/v4/schemas.pyi. These are only available in the stub package, so users of the stubs need to take care about where they use them. |
Yeah, that's not an unreasonable feature request. Are you interested in implementing this for mypy? |
I think that would depend on how challenging it is. Do you have some insight into what it would take to implement? |
Alas, I don't have time to look into that. I suggest that you try for about an hour and then report what you've learned. |
Mypy should inform users that |
This comment was marked as outdated.
This comment was marked as outdated.
@AlexWaygood This isn't a duplicate, |
Sorry, my bad, I misread! |
Feature
Currently Mypy doesn't seem to care whether a class is marked with
type_check_only
in a stub file. My proposal is that any runtime use of such a class should produce an error, including importing it outside ofif TYPE_CHECKING
and using it in annotations without quotes orfrom __future__ import annotations
.Pitch
To create stubs for libraries that define classes dynamically, it's pretty much required to create classes in stubs that don't exist at runtime. Marking these with
type_check_only
lets you show that these classes can't be used at runtime, but consumers of your stubs could easily still do so accidentally. It would be useful if Mypy could warn them in those cases.The text was updated successfully, but these errors were encountered: