python / python/mypy

Support file-level assertions as inline configuration

Open
#19,013 4 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature topic-configuration
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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 is win32 and the tuple sys.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 https://github.com/python/mypy/issues/19009. Brief recap:

When you have the following provider module:

if sys.platform == "win32":
    from my_pkg._pty.windows import PtySession
else:
    from my_pkg._pty.unix import PtySession

and attempt importing this symbol from somewhere else, Mypy does not respect the platform condition.

For example, if you call pty.openpty() inside my_pkg._pty.unix and run Mypy on Windows it will show:

Module has no attribute "openpty"  [attr-defined]

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 put assert 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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how Mypy handles existing top-level platform and Python-version assertions, then examine the configuration or preprocessing path where an inline file directive could be recognized. Compare the proposed two-variable evaluation with current platform configuration behavior. Done means a file-level assertion can conditionally enable checking without changing runtime behavior, with coverage for supported platform and version expressions.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers, devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.