-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Support file-level assertions as inline configuration #19013
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
It might be easier to support the following idiom: from typing import TYPE_CHECKING
if TYPE_CHECKING:
import sys
assert sys.platform == 'linux' as equivalent to top-level assert. It won't be a trivial change, but might still be easier than assert. (beware of https://peps.python.org/pep-0781/ - should it get accepted, |
That would indeed be nicer than the current situation but it still requires 3 lines of code and would still have to be inserted before imports that don't exist without the condition. |
I was hoping this would work but it doesn't. Compare https://mypy-play.net/?mypy=1.15&python=3.12&gist=b11740ebcbc73e8300ba64eb80eafd28 from typing import TYPE_CHECKING
if TYPE_CHECKING:
import sys
assert sys.platform == 'win32'
from ctypes import WinError main.py:8: error: Module "ctypes" has no attribute "WinError" [attr-defined]
Found 1 error in 1 file (checked 1 source file) To https://mypy-play.net/?mypy=1.15&python=3.12&gist=ebd7a55f45d19c31df4fd56d7911c5f3 import sys
assert sys.platform == 'win32'
from ctypes import WinError Success: no issues found in 1 source file I can't use the latter because it breaks Sphinx autodoc by raising an |
I found something that works a bit better. if TYPE_CHECKING:
if sys.platform != "win32":
assert False, "This backend is only available on Windows" But it doesn't suppress all errors, for example |
Feature
One would be able to do:
# mypy: assert=platform == "win32" and python_version >= "3.10"
and the file would only be checked if
sys.platform
iswin32
and the tuplesys.version_info
is greater than or equal to(3, 10)
. This is similar to the environment markers used in dependency definitions but with only support for the two static platform variables that are already used by Mypy.Implementation-wise, there would be a pre-processing step that transforms the Python version string (if present) into a tuple. This way you can pass those two variables directly to
eval
.Pitch
This would significantly help in situations such as #19009. Brief recap:
The best solution available is adding a top-level assertion as introduced here. For example, on top of the module
my_pkg._pty.unix
you would putassert sys.platform != "win32"
.This is suboptimal because it requires modifying runtime behavior. The
sys
import is required, perhaps only for the assertion, and if there are imports that only exist conditionally the assertion has to be in the middle of the standard import block on top.Adding a file-level comment is the most pragmatic option at hand, in lieu of adding an option that automatically propagates conditional static assertions to enclosed imports.
The text was updated successfully, but these errors were encountered: