Really strange behavior here, when this statement is on the module level it creates a silently unreachable branch, but when in a scope it doesn't create unreachable
Maybe it'd be good to refer to the latter as something different than "reachability"
So, except for this hack, which is intended to reduce the indent in this case
import sys
-if sys.platform == 'windows':- <rest of file>+assert sys.platform == 'windows'+<rest of file>
we don't treat platform/version checks as reachability cues at all (otherwise you'd have to play error whack-a-mole, since if your code has an if-else for two platforms, one would always be unreachable).
💡 Here's an idea: The #5894 feature is intended to designate an entire module as unfit on other platforms. One clearly has to import sys before this assertion, but it's probably not needed to execute any other statements except for imports, so how about:
Continue working as-is if it appears after import statements only.
If other statements were seen before it, then it'll be an error, e.g.
importsysdeff() ->None: ...
assertsys.platform=='windows'# E: Module-level platform assertions must appear at the beginning of the module (before other statements)
Activity
ikonst commented on Jun 12, 2023
We give special treatment to top-level assert statements here. This was discussed in #5308 and implemented in #5894.
The term "unreachable" is overloaded and refers to either:
Maybe it'd be good to refer to the latter as something different than "reachability"
So, except for this hack, which is intended to reduce the indent in this case
we don't treat platform/version checks as reachability cues at all (otherwise you'd have to play error whack-a-mole, since if your code has an if-else for two platforms, one would always be unreachable).
💡 Here's an idea: The #5894 feature is intended to designate an entire module as unfit on other platforms. One clearly has to import sys before this assertion, but it's probably not needed to execute any other statements except for imports, so how about: