-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Idea: allow assert with platform or version check at top of file #5308
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
I like it. Another (minor) problem with the approach of enclosing the entire file in |
I think this is a good idea. We also probably want to support this related idiom:
|
@msullivan We already support platform checks. So if I run this:
on Linux, I get |
I think giving an error when mypy finds a reachable Anyway, I just rechecked our original use case, and there we would actually want mypy to be silent if it finds a file containing So really all I want is for mypy to silently ignore the rest of a file when it encounters a top-level assert with a sys.platform check in it. |
@ethanhs When I check that, I get:
I don't mean giving an error on This is prompted by a request from an internal user trying to run mypy on multiple platforms with some code that looks like what I had above. |
I like the top-level assert idea. It looks like this is something we should prioritize? |
If it’s simple to do, yes.
|
This keeps coming, so I am raising priority to high. |
To be clear, the implementation we're looking for is
|
Would it be really lame if I implemented this only if the |
If that would be much easier implement than supporting this everywhere, I don' think that it would very lame. We'd just need to document it as a very specific feature -- ignoring the whole file (or anything after the assert) on some platform(s). |
I think it would be much easier. In semanal_pass1.py we can just truncate the list of defs when we encounter such an assert. |
- A top-level assert has no indentation. - An always-false condition is a check for sys.platform or sys.version_info or a condition derived from MYPY or from a name passed to --always-false; `assert False` doesn't count (!). Fixes python#5308
- An always-false condition is a check for `sys.platform` or `sys.version_info` or a condition derived from `MYPY` or `typing.TYPE_CHECKING` or from a name passed to `--always-false`. Note that `assert False` doesn't count (!). Fixes #5308
What do we think about this? #19013 |
Some files only work for certain platforms, but sometimes it's inconvenient to move the file to a location where it'll only be picked up for that platform. And yet the file doesn't type check cleanly on another platform (or ditto for Python versions).
We could enclose the entire file in
if sys.platform == 'win32':
but that seems overkill (and adding the indentation makes for an awkward diff). I propose an alternative that would have the same effect:assert sys.platform == 'win32'
.The text was updated successfully, but these errors were encountered: