Skip to content
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

Testing which type checker is type checking the code #4292

Closed
Dr-Irv opened this issue Dec 7, 2022 · 3 comments
Closed

Testing which type checker is type checking the code #4292

Dr-Irv opened this issue Dec 7, 2022 · 3 comments
Labels
enhancement request New feature or request

Comments

@Dr-Irv
Copy link

Dr-Irv commented Dec 7, 2022

Is your feature request related to a problem? Please describe.

In testing pandas-stubs, we have code that fails mypy (because of a mypy bug), but not pyright. If we use #type: ignore comments to suppress the mypy errors, it also suppresses the pyright errors. Turns out that in one case, pyright identified an error that we should fix without the #type: ignore comments.

Ideally, there would be some way of testing which type checker is being used (or mypy could implement a # mypy: ignore, but that doesn't seem to be happening any time soon - python/mypy#12358)

Describe the solution you'd like

if TYPE_CHECKING and TYPE_CHECKER == "pyright":
    # Some code that only pyright will check

I realize that this kind of thing won't work at runtime or with another type checker, but I'm looking for a solution where I could say "pyright should type check this, but other type checkers should not", so I'm open to other ideas.

The only thing I've thought of is to have a constant that we import as TYPE_CHECKER, and then we patch that file in our CI before running each type checker.

Has the typing community considered adding this feature (having a constant that indicates the label of the type checker) as a possible PDEP?

@Dr-Irv Dr-Irv added the enhancement request New feature or request label Dec 7, 2022
@erictraut
Copy link
Collaborator

I'm not aware of any discussions in the typing community about exposing the type checker to the code being type checked. There was a discussion a few years ago about exposing a way for code to determine which type checking features were supported by the type checker (e.g. ParamSpec or Self), but that discussion never converged.

You can effectively implement the solution you're proposing by defining your own value of TYPE_CHECKER (or whatever other name you'd like to chooose). Both mypy and pyright have the ability to define constant values as part of their configurations. In the case of pyright, you would use the defineConstant configuration setting. That would look something like: { "defineConstant": { "TYPE_CHECKER": "pyright" } }.

Another option is to set enableTypeIgnoreComments to false when running pyright. This tells it to effectively ignore # type: ignore comments but still honor # pyright: ignore comments.

I think that a # mypy: ignore is sorely needed and the preferable solution in this case. Perhaps you could lobby the mypy maintainers to add it. You might even consider submitting a PR that implements the functionality.

@Dr-Irv
Copy link
Author

Dr-Irv commented Dec 7, 2022

You can effectively implement the solution you're proposing by defining your own value of TYPE_CHECKER (or whatever other name you'd like to chooose). Both mypy and pyright have the ability to define constant values as part of their configurations. In the case of pyright, you would use the defineConstant configuration setting. That would look something like: { "defineConstant": { "TYPE_CHECKER": "pyright" } }.

This looks like a good solution for us.

I think that a # mypy: ignore is sorely needed and the preferable solution in this case. Perhaps you could lobby the mypy maintainers to add it. You might even consider submitting a PR that implements the functionality.

I agree, but based on the discussion in python/mypy#12358 , it requires changes to ast, so this is not an easy change.

@erictraut
Copy link
Collaborator

A change to the AST isn't required to implement # mypy: ignore. As suggested in that other thread, it would require mypy to implement a reg-ex text search to detect these comments without the use of the AST.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants