Skip to content

Update specification for directives for sys.implementation and sys.platform checks. - #2173

Open
Josverl wants to merge 7 commits into
python:mainfrom
Josverl:specs/sys.implementation.name
Open

Josverl wants to merge 7 commits into
python:mainfrom
Josverl:specs/sys.implementation.name

Conversation

@Josverl

@Josverl Josverl commented Feb 12, 2026 •

Copy link
Copy Markdown

This PR expands and clarifies the specification for version, platform, and Python implementation checks:

  • Adds checks for sys.implementation.name and sys.implementation.version.
  • Specifies supported comparisons, including in / not in with literal tuples, sets, and lists, and sys.platform.startswith().
  • Covers combining checks with and, or, and not.
  • Distinguishes required forms from optional extensions.
  • Adds examples and guidance on target configuration and defaults.

References :

@python-cla-bot

python-cla-bot Bot commented Feb 12, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
@srittau srittau added the topic: typing spec For improving the typing spec label Feb 20, 2026
@Josverl
Josverl force-pushed the specs/sys.implementation.name branch 2 times, most recently from 4151e84 to d7fbb9e Compare February 21, 2026 17:09
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
@Josverl
Josverl force-pushed the specs/sys.implementation.name branch from d7fbb9e to 62d90dc Compare February 22, 2026 12:56
@Josverl

Josverl commented Feb 22, 2026 •

Copy link
Copy Markdown
Author

I have updated the text to better clarify the exact comparisons for each of the supported attributes,
excluded the use of named attributes,
and did some more word-smithing to reduce unneeded verbosity.
I have also added highlighting to the examples, as a raiding aide.

@Josverl
Josverl force-pushed the specs/sys.implementation.name branch from 62d90dc to ff338e3 Compare February 22, 2026 13:08
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
@Josverl

Josverl commented Mar 30, 2026 •

Copy link
Copy Markdown
Author

Dear TC,
Is there a decision on this PR yet, or have I missed a step in the process?

If so kindly let me know how to proceed.

@JelleZijlstra JelleZijlstra left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm generally supportive but I think this change needs some more editing, and I'd like support in at least some type checkers (at least an open PR) before we start mandating this pattern.

Comment thread docs/spec/directives.rst Outdated
@Josverl
Josverl force-pushed the specs/sys.implementation.name branch from c575c15 to c951b00 Compare April 3, 2026 19:21
@Josverl
Josverl requested review from carljm and erictraut April 3, 2026 19:23
@Josverl

Josverl commented Aug 9, 2026 •

Copy link
Copy Markdown
Author

Dear typing council,
I have been waiting patiently , but there seems to be no progress on this.
As far as I can see I have actioned - or responded - to all feedback,
Reviewers have not responded to the updates they requested. Eric has stepped down for the TC since - so that is understandable.

However now it appears that this PR is in a Catch-22 situation;
Waiting for One or more typecheckers to implement this - but for typecheckers to implement this it should be in the spec.

Lacking response I plan close this in a few weeks

Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
@davidhalter

davidhalter commented Aug 9, 2026 •

Copy link
Copy Markdown
Collaborator

Waiting for One or more typecheckers to implement this - but for typecheckers to implement this it should be in the spec.

This is not the case at all. It is very typical for type checkers to implement features even if they are not in the spec. It is very helpful to have feedback from type checker authors to be able to understand potential problems with spec changes.

I think this has worked quite well in the past.and if a feature is useful enough there is usually a type checker that will implement it.

@carljm carljm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your patience, @Josverl. I'm not sure we've fully reached consensus yet on what should be supported here, but I also generally support making this clearly specified. I also think this still needs some editing to give a clear and unambiguous presentation of the requirements.

Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
@Josverl

Josverl commented Aug 10, 2026

Copy link
Copy Markdown
Author

Thanks all for the renewed attention and comments.

  • I'll try to clarify where I can do
  • and keep a list of topics that need a decision one way or another

Josverl and others added 4 commits August 15, 2026 22:15
…atform checks.

Signed-off-by: Jos Verlinde <Jos.Verlinde@Microsoft.com>
Formatting improvements.

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
…sion comparisons.

Signed-off-by: Jos Verlinde <Jos_Verlinde@hotmail.com>
Signed-off-by: Jos Verlinde <Jos_Verlinde@hotmail.com>
@Josverl
Josverl force-pushed the specs/sys.implementation.name branch from b3b76c1 to 98e3697 Compare August 15, 2026 20:18
@Josverl

Josverl commented Aug 15, 2026 •

Copy link
Copy Markdown
Author

Again thanks for the feedback and suggestions to improve the spec.

I processed the comments and suggestion - and commented or marked them as resolved to keep track.
Please feel free to Unresolve or add additional comments as needed.
I have extended the Configuration section to help clarify how a type checker could be configured including default values or approaches for defaults.

Open topics.

1. 2-Tuple or 3-Tuple version comparisons.
see comments

  • requirement is for 2-Tuple checks
  • type checkers are allowed to also process 3-Tuple checks

2. Should in / not in membership checks use tuple, set or list.
intent was tuples, was updated to set , now waiting for consensus

* ``sys.platform in <tuple/set/list of string literals>``
* ``sys.platform not in <tuple/set/list of string literals>``
* ``sys.implementation.name in <tuple/set/listof string literals>``
* ``sys.implementation.name not in <tuple/set/list of string literals>``

3. Scope
as raised by @AlexWaygood
Is the scope of this change too large (it has grown since that comment) and should there be a implementation timeline / staged implementation.
I have refrained from answering this - as I am not the one that can do the implementation.

I think the commits should be squashed when merging - I've kept them separate for now.

@carljm

carljm commented Aug 17, 2026

Copy link
Copy Markdown
Member

We could eliminate open-question (2) and at least partially address (3) by just considering containment checks as out of scope to specify for now. Considering they don't add any new capability, just allow writing more concise code, they aren't really necessary. We could leave this out of the spec / conformance suite for now and up to individual type checkers. Similarly maybe for startswith. Leaving it out doesn't prevent type checkers from supporting it. And including it in the spec / conformance suite doesn't guarantee type-checker support, either, so it's unclear how soon some of these difficult-to-support patterns will be usable in practice anyway, even if they are included here.

@Josverl Josverl reopened this Aug 17, 2026
@Josverl

Josverl commented Aug 17, 2026

Copy link
Copy Markdown
Author

I think that we should keep startswith.

Is there any type checker that support this currently?

@AlexWaygood

Copy link
Copy Markdown
Member

I think that we should keep startswith.

Is there any type checker that support this currently?

ty

@carljm

carljm commented Aug 17, 2026 •

Copy link
Copy Markdown
Member

At least for ty, startswith support is trivial (already there!), don't know how difficult for other type checkers. Point taken that (unlike containment checks) it really does introduce a new (and sometimes needed capability).

Regarding containment checks, for ty tuples are trivial (also already supported!) but mutable types are not. ty handles this quite differently from other type checkers: we use full type inference, not special-cased AST-matching. This makes some thing a lot easier, and some things harder. For mutable container types, we would normally not infer e.g. ["foo", "bar"] as list[Literal["foo", "bar"]] but as list[str] (same for set), so we normally would lose the literal precision. It should be doable, just not straightforward and I'm not sure how quickly we'd prioritize it. (But once we did support it, we would support it for all containment checks of a literal type against a literal list/set of literal types, not just for sys.platform and friends specifically.)

I suspect that for type checkers that do AST matching, containment checks for tuple/list/set should all be equally straightforward.

@jorenham

Copy link
Copy Markdown
Collaborator

Regarding containment checks, for ty tuples are trivial (also already supported!) but mutable types are not. ty handles this quite differently from other type checkers: we use full type inference, not special-cased AST-matching.

Would frozenset help?

@carljm

carljm commented Aug 17, 2026 •

Copy link
Copy Markdown
Member

Would frozenset help?

Not much. It would require that we implement astral-sh/ty#2280, which probably mostly requires the same machinery. (The problem here is that frozenset doesn't have a literal syntax, so first you have to build a literal container of some other kind, and then pass it to the frozenset constructor, so we still end up promoting literals in that process.)

To be clear, I don't want you to weight this ty implementation issue too much -- I'd rather first hear from other typing council members what they think a good scope is here.

@Josverl

Josverl commented Aug 28, 2026

Copy link
Copy Markdown
Author

Gentle reminder for additional feedback.

Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst
Comment thread docs/spec/directives.rst Outdated
@JelleZijlstra

Copy link
Copy Markdown
Member

Here's a summary of what type checkers already support (courtesy of Codex):

Proposed behavior mypy Pyright ty Pyrefly
sys.version_info >= (M, m) and < (M, m) Yes Yes Yes Yes
sys.platform == literal and != literal Yes Yes Yes Yes
sys.platform.startswith(literal) Yes No Yes Yes
sys.platform in tuple No No Yes Yes
sys.platform not in tuple No No Yes Yes
sys.implementation.name == / != No No No No
sys.implementation.name in / not in No No No No
sys.implementation.version >= / < No No No No
Combining recognized checks with not, and, and or Yes Yes Yes Yes
Configure target Python version Yes Yes Yes Yes
Configure target sys.platform Yes Partial Yes Yes
Configure implementation name or version No No No No

So nobody supports any sys.implementation checks. Support for slightly more advanced sys.platform checks is a bit varied. I had Codex look through past GitHub issues and it wasn't able to find much demand for in/not in checks, which makes me think that part is maybe not worth the trouble. On the other hand, ty and Pyrefly already support those (in Pyrefly's case, apparently as a result of some of the discussion around this PR), and it doesn't seem like an onerous feature to add.

hauntsaninja pushed a commit to python/mypy that referenced this pull request Sep 1, 2026
This is proposed for the spec in python/typing#2173. It is trivial to
support in
mypy and seems useful, so let's add it.
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Also :
-  wrapped lines to ~85 characters
- tables to 110 characters
- Improved .rst formatting and markup

The Intent is for these commits allow tracability of review comments,
but to be squashed to a single commit during merge.

Signed-off-by: Jos Verlinde <Jos_Verlinde@hotmail.com>
carljm added a commit to astral-sh/ruff that referenced this pull request Sep 21, 2026
Inline list and set membership tests now preserve the values of their
individual elements, allowing ty to determine when a condition is always
true or false. With the target platform set to Linux, both `sys.platform
in ["linux", "windows"]` and `sys.platform in {"linux", "windows"}`
select only the matching branch, so an assignment there is inferred as
`Literal[1]`.

Reuse the precise element extraction used for iteration and membership
narrowing without changing the inferred container type. Lists and sets
use the existing tuple membership equality evaluator, including its
identity-or-equality behavior. For sets, assume equality is an
equivalence relation and equal objects have equal hashes. Checking
reflexive equality across union alternatives also improves existing
tuple membership inference.

Context is that in python/typing#2173 people are
wanting to specify that `sys.platform in ["linux", "windows"]` should
work. This makes it work in ty so that we can be fine with any outcome
of that specification change.

## Test plan

Mdtests cover platform-dependent branch reachability; positive and
negative membership in inline lists and sets; inferred literal values
and unions; fixed-length unpacking; stored mutable containers; custom
equality and identity; comparison chains; and preservation of enum-set
narrowing.

## Ecosystem report

The comparison for head `90111f10da9359722abb903351b89aaae0f9bcba` adds
four `redundant-condition-strict` warnings. The paasta and SciPy
warnings correctly identify disjoint membership checks. The dd-trace-py
warning exposes existing limitations in modeling dynamic `setattr`; the
Scrapy warning exposes existing attribute narrowing across method calls
and `await`. Equivalent equality checks already exhibit both limitations
on the base revision. These existing limitations are accepted for this
change. See the [full ecosystem
report](#28676 (comment))
for minimized examples and exact comparison details.
@Josverl

Josverl commented Oct 7, 2026

Copy link
Copy Markdown
Author

Just to avoid unneeded waiting:
I think I have responded to all review comments; but have not marked them as resolved (as earlier I was to eager to close)
Please let me know if further clarifications or updates are requested, or that things are clear.

Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
@Josverl

Josverl commented Oct 19, 2026 •

Copy link
Copy Markdown
Author

@carljm,
Sorry for the delay in addressing your latest suggestions & comments.

CI also seems to have forgotten that I did sign the CLA with all my identities, re-signing is not possible either.
Not sure what I can do about that.

@carljm carljm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the CLA signing failure is because your latest commit (unlike the previous ones) is authored by Jos_Verlinde@hotmail.com, which GitHub can't associate with your GitHub account. If you can amend that commit with a recognized email address and force push, I suspect the CLA signing check will pass.

Barring my two nits below, I think this PR is ready for conformance tests. @jorenham @davidhalter @JelleZijlstra @rchen152 any objections to the spec updates here, before @Josverl writes conformance tests?

Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
@jorenham

Copy link
Copy Markdown
Collaborator

I'll try to take a look this evening

@jorenham jorenham left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall, thanks. I left some comments for some minor issues / nits / suggestions.

Comment thread docs/spec/directives.rst
Required forms and optional extensions
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Type checkers must support the forms specified above as required. Support for additional

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consistency nit

Suggested change
Type checkers must support the forms specified above as required. Support for additional
Type checkers should support the forms specified above as required. Support for additional

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think that "should" can be combined with "required" in this sentence.
That would degrade the meaning of the "required" statements in the prior paragraphs, which I think is incorrect.

Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst Outdated
Comment thread docs/spec/directives.rst
@Josverl
Josverl force-pushed the specs/sys.implementation.name branch from d027211 to bc8965e Compare October 21, 2026 21:13
Minor changes to wording and formatting.

Signed-off-by: Jos Verlinde <jos_verlinde@hotmail.com>

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic: typing spec For improving the typing spec

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants