Skip to content

[ty] Fix identity narrowing for NewTypes#26439

Draft
charliermarsh wants to merge 27 commits into
mainfrom
charlie/issue-3552-newtype-is-narrowing
Draft

[ty] Fix identity narrowing for NewTypes#26439
charliermarsh wants to merge 27 commits into
mainfrom
charlie/issue-3552-newtype-is-narrowing

Conversation

@charliermarsh

@charliermarsh charliermarsh commented Jun 28, 2026

Copy link
Copy Markdown
Member

Summary

NewType is nominal for static typing, but its constructor returns the input object unchanged at runtime. Prior to this change, we used ordinary type disjointness to evaluate and narrow is comparisons, so two distinct NewTypes with the same concrete base could be treated as unable to share an object and valid branches narrowed to Never:

from typing import NewType

class Foo: ...

FooNewType1 = NewType("FooNewType1", Foo)
FooNewType2 = NewType("FooNewType2", Foo)

foo = Foo()
foo1 = FooNewType1(foo)
foo2 = FooNewType2(foo)

reveal_type(foo1 is foo2)  # main: Literal[False]; this PR: bool
if foo1 is foo2:  # True at runtime
    reveal_type(foo1)  # main: Never; this PR: FooNewType1
    reveal_type(foo2)  # main: Never; this PR: FooNewType2

This PR projects NewTypes to their runtime representations before evaluating identity.

Closes astral-sh/ty#3552.

@astral-sh-bot astral-sh-bot Bot added the ty Multi-file analysis & type inference label Jun 28, 2026
@astral-sh-bot

astral-sh-bot Bot commented Jun 28, 2026

Copy link
Copy Markdown

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 94.47%. The percentage of expected errors that received a diagnostic held steady at 89.19%. The number of fully passing files held steady at 95/134.

@charliermarsh charliermarsh added the bug Something isn't working label Jun 28, 2026
@astral-sh-bot

astral-sh-bot Bot commented Jun 28, 2026

Copy link
Copy Markdown

Memory usage report

Summary

Project Old New Diff Outcome
prefect 449.23MB 449.28MB +0.01% (44.93kB)
sphinx 167.07MB 167.08MB +0.01% (10.05kB)
trio 70.47MB 70.47MB +0.01% (4.32kB)
flake8 29.02MB 29.03MB +0.01% (1.77kB)

Significant changes

Click to expand detailed breakdown

prefect

Name Old New Diff Outcome
all_narrowing_constraints_for_expression 5.22MB 5.26MB +0.77% (41.12kB)
infer_expression_types_impl 37.97MB 37.97MB +0.00% (1.26kB)
infer_definition_types 49.99MB 49.99MB +0.00% (784.00B)
UnionType 1.07MB 1.07MB +0.07% (736.00B)
is_redundant_with_impl::interned_arguments 2.35MB 2.35MB +0.01% (352.00B)
is_redundant_with_impl 1.31MB 1.31MB +0.01% (192.00B)
IntersectionType<'db>::from_two_elements_::interned_arguments 13.32kB 13.49kB +1.29% (176.00B)
loop_header_reachability 254.53kB 254.70kB +0.06% (168.00B)
analyze_non_terminal_call 1.78MB 1.78MB +0.01% (112.00B)
IntersectionType<'db>::from_two_elements_ 9.74kB 9.84kB +0.96% (96.00B)

sphinx

Name Old New Diff Outcome
all_narrowing_constraints_for_expression 1.94MB 1.95MB +0.50% (9.92kB)
infer_expression_types_impl 14.89MB 14.89MB -0.00% (576.00B)
IntersectionType 534.62kB 535.09kB +0.09% (480.00B)
UnionType 583.61kB 583.95kB +0.06% (352.00B)
is_redundant_with_impl::interned_arguments 1.14MB 1.14MB +0.03% (352.00B)
infer_definition_types 13.69MB 13.69MB -0.00% (336.00B)
is_redundant_with_impl 631.71kB 631.96kB +0.04% (256.00B)
analyze_non_terminal_call 683.03kB 682.81kB -0.03% (224.00B)
loop_header_reachability 228.74kB 228.58kB -0.07% (168.00B)

trio

Name Old New Diff Outcome
all_narrowing_constraints_for_expression 462.36kB 466.50kB +0.90% (4.14kB)
infer_expression_types_impl 4.42MB 4.42MB +0.00% (112.00B)
infer_definition_types 3.97MB 3.97MB +0.00% (48.00B)
loop_header_reachability 71.24kB 71.26kB +0.03% (24.00B)

flake8

Name Old New Diff Outcome
all_narrowing_constraints_for_expression 74.68kB 76.45kB +2.36% (1.77kB)

@astral-sh-bot

astral-sh-bot Bot commented Jun 28, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Flaky changes detected. This PR summary excludes flaky changes; see the HTML report for details.

Full report with detailed diff (timing results)

@charliermarsh charliermarsh force-pushed the charlie/issue-3552-newtype-is-narrowing branch from 787ba02 to 523fa56 Compare June 28, 2026 21:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect is narrowing of code involving newtypes

1 participant