-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Support conditional Python version and platform checks #698
Comments
We should also support code that assigns the result of a check to a variable. Example: import sys
PY3_OR_LATER = sys.version_info[0] >= 3
if PY3_OR_LATER:
...
else:
... |
How about type comment? def quote(s):
# if PY2:
# type: (AnyStr) -> AnyStr
# else:
# type: (str) -> str
return '"' + s + '"' or def quote(s):
if PY2:
# type: (AnyStr) -> AnyStr
...
else:
# type: (str) -> str
...
return '"' + s + '"' |
Please no new proposals. |
Cases like the one above could be handled already by defining a type variable or synonym |
I'm sorry, and thanks for information. |
Note that different versions may have different signatures for the same function. This should definitely be supported. |
My plan:
I may need help with the last item, and maybe we should update PEP 484 to clarify what's allowed. |
Here are some thoughts:
|
Good to know. Are there any existing helper functions that might be useful
to pick apart things like tuples of int literals or e.g.
sys.version_info[:2]? (I'm not afraid to hand-code these, but if something
relevant already exists I'd like to use it.)
|
There's |
Addresses most but not all of #698. (The first two bullets of my plan.)
I did the first two bullets of my plan, and filed separate issues for the last two (the last one I think we may not actually need). So I'm closing this. |
Mypy should support some common kinds of Python version checks and platform checks (e.g. Windows vs. Posix). We should ignore code paths that won't be run on the targeted Python version or platform. This way mypy can more effectively type check code that supports Python 2 and 3 and multiplatform code.
We first need to decide which checks to support. These examples are from PEP 484 and should be supported:
When type checking code as above, always only if or the else block would be analyzed, never both, since on any given program run only one them can be evaluated (we assume that nobody does anything crazy like modifying
sys.platform
at runtime). We'd detect the check expressions during semantic analysis and wouldn't semantically analyze (or type check) the skipped blocks, similar to how mypy currently deals withPY2
/PY3
conditions in if statements.We already have Python 2 and Python 3 modes, and we should also implement Windows and non-Windows (Posix) modes. Initially, we can just use the platform on which the type checker is being run, but more generally this should be configurable (e.g.,
mypy --platform win32 ...
).The text was updated successfully, but these errors were encountered: