Latest Results
[`pycodestyle`] Exempt module-level `sys.platform` assertions (`E402`)
`assert sys.platform == "darwin"` before an import is a common way to
guard platform-specific imports, since a type checker treats it as
making the rest of the module unreachable elsewhere. E402 flagged the
imports that followed, while the equivalent `if sys.platform != ...:
raise` form was already exempt because a top-level `if` block never
ends the import section.
Accept the checks mypy narrows on regardless of target platform:
`sys.platform == "..."`, `sys.platform != "..."`, and
`sys.platform.startswith("...")`, alone or combined with `and`, `or`,
and `not`. Near-misses are rejected because mypy leaves the module
reachable for them — membership tests, a tuple of prefixes, a
non-literal operand, reversed operands, chained comparisons, and
`sys` reached through an alias or `from sys import platform`.
Requiring every leaf of the boolean tree to be a platform check is
stricter than mypy, which narrows `sys.platform == "win32" and f()`
when checking a non-Windows target; a linter has no target platform
to reason from.
Closes #26160
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>Avasam:Support-mypy-module-level-platform-assertions-for-module-import-not-at-top-of-file-(E402) Latest Branches
0%
Avasam:Ignore-import-private-name-(PLC2701)-in-stubs 0%
Avasam:Support-mypy-module-level-platform-assertions-for-module-import-not-at-top-of-file-(E402) +28%
denyszhak:ty/skip-redundancy-check-for-simplified-unions © 2026 CodSpeed Technology