Skip to content

Avoid narrowing to NewType#20766

Merged
hauntsaninja merged 1 commit intopython:masterfrom
hauntsaninja:narrow90
Feb 14, 2026
Merged

Avoid narrowing to NewType#20766
hauntsaninja merged 1 commit intopython:masterfrom
hauntsaninja:narrow90

Conversation

@hauntsaninja
Copy link
Collaborator

@hauntsaninja hauntsaninja commented Feb 8, 2026

Fixes #20733

I might make this type munging look a little nicer in a future refactor

This introduces new errors in two primer projects

@github-actions
Copy link
Contributor

github-actions bot commented Feb 8, 2026

Diff from mypy_primer, showing the effect of this PR on open source code:

steam.py (https://github.com/Gobot1234/steam.py)
+ steam/ext/csgo/state.py:213: error: Argument 1 to "__delitem__" of "dict" has incompatible type "int"; expected "AssetID"  [arg-type]
+ steam/ext/csgo/state.py:214: error: Argument 1 to "__delitem__" of "dict" has incompatible type "int"; expected "AssetID"  [arg-type]

rotki (https://github.com/rotki/rotki)
- rotkehlchen/chain/evm/decoding/zerox/decoder.py:146: error: Unused "type: ignore" comment  [unused-ignore]
- rotkehlchen/chain/ethereum/modules/sky/decoder.py:147: error: Unused "type: ignore" comment  [unused-ignore]

@hauntsaninja
Copy link
Collaborator Author

rotki was fixed in #20492 and is regressed here
steam was fixed in #20602 and is regressed here

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

Are you planning to address the regressions in this PR, or should we merge this as is, since this fixes a more important regression?


def f2(whatever: object, uid: UserId | Other):
if whatever == uid:
reveal_type(whatever) # N: Revealed type is "builtins.int | __main__.Other"
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a bit questionable, since whatever could be float or another numeric type that happens to compare equal to int (not directly related to the changes in this PR).

@hauntsaninja
Copy link
Collaborator Author

hauntsaninja commented Feb 11, 2026

The regressions are from what the PR does (no longer narrowing to a NewType), so nothing to address beyond deciding what behaviour we want mypy to have.

(I don't have a strong opinion / am not a big NewType user myself. Allowing narrowing is good on primer and the implementation is simpler and consistent with subclassing, but I can see the argument made in #20733 about having to be explicit)

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

I like that NewTypes require an explicit constructor call.

@hauntsaninja hauntsaninja changed the title Avoid narrowing NewType Avoid narrowing to NewType Feb 12, 2026
@hauntsaninja hauntsaninja merged commit a2d715a into python:master Feb 14, 2026
23 checks passed
@hauntsaninja hauntsaninja deleted the narrow90 branch February 14, 2026 01:40
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.

Regression: Type of literal narrowed when asserting equality to a NewType

2 participants