Skip to content

Conformance: annotate module protocol globals#2318

Merged
carljm merged 1 commit into
python:mainfrom
charliermarsh:charlie/optional-module-protocol-diagnostic
Jul 6, 2026
Merged

Conformance: annotate module protocol globals#2318
carljm merged 1 commit into
python:mainfrom
charliermarsh:charlie/optional-module-protocol-diagnostic

Conversation

@charliermarsh

@charliermarsh charliermarsh commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

This test is intended to verify that a module can implement a protocol when its public interface is compatible. As written, though, it also prescribes how type checkers infer the public types of unannotated module globals.

The protocol attributes here are mutable, so their types are invariant. Some type checkers widen an assignment like one_flag = True to bool when accessed from another module, but in ty, we retain Literal[True], in which case the attribute is not writable as an arbitrary bool.

Explicitly annotating these variables as int and bool makes the intended public interface unambiguous (and should keep all existing implementations in-conformance).

The module protocol test should exercise structural compatibility without prescribing how type checkers infer unannotated module globals. Give the mutable attributes explicit public types so their protocol compatibility is unambiguous.
@charliermarsh charliermarsh marked this pull request as ready for review July 6, 2026 23:50
@carljm carljm merged commit f4f2952 into python:main Jul 6, 2026
5 checks passed
charliermarsh added a commit to astral-sh/ruff that referenced this pull request Jul 7, 2026
Update the `python/typing` pin to the latest main. This picks up
python/typing#2318, which annotates module
protocol globals so the conformance test exercises structural
compatibility without depending on inference for unannotated globals.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants