-
Notifications
You must be signed in to change notification settings - Fork 288
Update specification for directives for sys.implementation and sys.platform checks. #2173
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
Open
Josverl
wants to merge
1
commit into
python:main
Choose a base branch
from
Josverl:specs/sys.implementation.name
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -145,8 +145,27 @@ left undefined by the typing spec at this time. | |
| Version and platform checking | ||
| ----------------------------- | ||
|
|
||
| Type checkers are expected to understand simple version and platform | ||
| checks, e.g.:: | ||
| Type checkers should support narrowing based on: | ||
| * ``sys.version_info`` | ||
| * ``sys.platform`` | ||
| * ``sys.implementation.version`` | ||
| * ``sys.implementation.name`` | ||
|
|
||
| The `<comparison>` patterns for these variables are described in more detail in the following paragraphs. | ||
|
|
||
| sys.version_info checks | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| Type checkers should support the following ``sys.version_info`` comparison patterns: | ||
| * ``sys.version_info <comparison> <2-tuple>`` | ||
|
|
||
| `<comparison>` can be one of the following: | ||
| * Greater or equal: ``>=`` | ||
| * Less than: ``<`` | ||
|
|
||
| Comparisons checks are only supported against the first two elements of a tuple. | ||
|
|
||
| Example:: | ||
|
|
||
| import sys | ||
|
|
||
|
|
@@ -155,13 +174,117 @@ checks, e.g.:: | |
| else: | ||
| # Python 3.11 and lower | ||
|
|
||
|
|
||
|
|
||
| sys.platform checks | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| Type checkers should support ``sys.platform`` comparisons: | ||
|
|
||
| Supported patterns: | ||
| * ``sys.platform <comparison> <string literal>`` | ||
| * ``sys.platform in <tuple of string literals>`` | ||
|
|
||
| `<comparison>` can be one of the following: | ||
| * Equality: ``==`` | ||
| * Inequality: ``!=`` | ||
| * Membership: ``in`` | ||
|
|
||
| Example:: | ||
|
|
||
| import sys | ||
|
|
||
| if sys.platform == 'win32': | ||
| # Windows specific definitions | ||
| else: | ||
| # Posix specific definitions | ||
|
|
||
| Don't expect a checker to understand obfuscations like | ||
| ``"".join(reversed(sys.platform)) == "xunil"``. | ||
| if sys.platform in ("linux", "darwin"): | ||
| # Platform-specific stubs for Linux and macOS | ||
| ... | ||
|
|
||
|
|
||
| .. note:: | ||
|
|
||
| Type checkers are only required to support the above patterns. | ||
|
|
||
| For example, the pattern ``sys.platform == "linux"`` is supported but other syntax variants such as ``platform == "linux"`` and ``"win" not in sys.platform`` are not supported. | ||
| The membership check ``sys.platform in ("linux", "darwin")`` only supports simple containment testing to a tuple of literal strings. | ||
|
|
||
| sys.implementation.name checks | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| Type checkers should support ``sys.implementation.name`` comparisons: | ||
|
|
||
| Supported patterns: | ||
| * ``sys.implementation.name <comparison> <string literal>`` | ||
| * ``sys.implementation.name in <tuple of string literals>`` | ||
|
|
||
| `<comparison>` can be one of the following: | ||
| * Equality: ``==`` | ||
| * Inequality: ``!=`` | ||
| * Membership: ``in`` | ||
|
|
||
| Common values: ``"cpython"``, ``"pypy"``, ``"micropython"``, ``"graalpy"``, ``"jython"``, ``"ironpython"`` | ||
|
|
||
| Example:: | ||
|
|
||
| import sys | ||
| if sys.implementation.name == "cpython": | ||
| # CPython-specific stub | ||
| if sys.implementation.name == "micropython": | ||
| # MicroPython-specific stub | ||
|
|
||
|
|
||
| sys.implementation.version checks | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| Type checkers should support the following ``sys.implementation.version`` comparison patterns: | ||
| * ``sys.implementation.version <comparison> <2-tuple>`` | ||
|
|
||
| `<comparison>` can be one of the following: | ||
| * Greater or equal: ``>=`` | ||
| * Less than: ``<`` | ||
|
|
||
| Example:: | ||
|
|
||
| import sys | ||
|
|
||
| if sys.implementation.name == "pypy" and sys.implementation.version >= (7, 3): | ||
| # PyPy version 7.3 and above | ||
|
|
||
| if sys.implementation.name == "micropython" and sys.implementation.version >= (1, 24): | ||
| # MicroPython version 1.24 and above | ||
|
|
||
| .. note:: | ||
|
|
||
| Comparisons checks are only supported against the first two elements of a tuple. | ||
|
|
||
| ``sys.implementation.version`` is a tuple, in the same format as sys.version_info. However it represents the version of the Python implementation rather than the version of the Python language. | ||
| This has a distinct meaning from the specific version of the Python language to which the currently running interpreter conforms. | ||
|
|
||
|
|
||
| Combining checks | ||
| ^^^^^^^^^^^^^^^^ | ||
|
|
||
| Multiple comparisons can be combined with: | ||
| * A ``not`` unary operator | ||
| * An ``and`` or ``or`` binary operator | ||
|
|
||
| No support for complex expressions | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| Type checkers are not required to evaluate complex expressions involving these variables. | ||
| Therefore do not expect a checker to understand obfuscations such as:: | ||
|
|
||
| import sys | ||
| if "".join(reversed(sys.platform)) == "xunil": | ||
| # Linux specific code | ||
|
|
||
|
|
||
| Configuration | ||
| ^^^^^^^^^^^^^ | ||
|
|
||
| Type checkers should provide configuration to specify target version, platform, and implementation. The exact mechanism is implementation-defined. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Target version to what granularity?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair point, I think; as you suggested; Major.Minor |
||
|
|
||
|
|
||
| .. _`deprecated`: | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think adding this has does have significant ecosystem costs (as outlined by @erictraut in https://discuss.python.org/t/proposal-to-improve-support-for-other-python-platforms-in-the-typing-specification/91877/3 ).
In the end I think it is probably worth it, because without it, type-checking for alternative implementations of Python seems quite difficult. The cost to type checkers themselves seems relatively low: one new config option, defaulting to
"cpython".There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ecosystem is larger than just one implementation.
But I know I can't properly assess the effort needed to update and maintain the tooling I hope to influence.
So I'll leave the weighing up to those who can.