Skip to content

[ty] Constrain inferred constructor results using the initializer receiver - #30270

Draft
sharkdp wants to merge 1 commit into
mainfrom
codex/ty-4977-result-constraint
Draft

sharkdp wants to merge 1 commit into
mainfrom
codex/ty-4977-result-constraint

Conversation

@sharkdp

@sharkdp sharkdp commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Summary

If Reader.__init__ annotates self as Reader[bool | None], assigning Reader() to a variable of type Reader[bool] can silently drop None.

Alternative to #30179 for astral-sh/ty#4977. This approach keeps the constructor result generic during contextual inference. The receiver argument already requires Reader[T] to be assignable to Reader[bool | None]. When inferring a bare constructor against an expected type, also require Reader[bool | None] to be assignable to the inferred result Reader[T]. For a covariant reader this supplies the missing lower bound on T, so the assignment reports invalid-assignment.

For an inherited initializer, first express the annotated result as a specialization of the subclass. Type arguments that the initializer leaves open can still come from the assignment. This also preserves the existing inference of X[Any] for x: X[Any] = X() with self: X[None].

Test plan

Regression cases cover mixed-variance fields in both generic syntaxes, explicit specialization, and inherited initializers.

@sharkdp sharkdp added bug An issue describing something that isn't working, or a PR that fixes a bug ty The ty type checker labels Nov 10, 2026
@sharkdp
sharkdp deployed to automations November 10, 2026 15:44 — with GitHub Actions Active
@astral-automations-bot

Copy link
Copy Markdown
Contributor

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 98.60%. The percentage of expected errors that received a diagnostic held steady at 98.86%. The number of fully passing files held steady at 137/146.

Results for 4a8dbdc · Workflow run

@astral-automations-bot

Copy link
Copy Markdown
Contributor

Memory usage report

Summary

Project Old New Diff Outcome
trio 90.84MB 95.79MB +5.45% (4.95MB) ⏫
prefect 446.64MB 446.64MB +0.00% (1.10kB) ⏫
flake8 36.01MB 36.01MB - ✅
sphinx 176.13MB 176.13MB - ✅

Significant changes

Click to expand detailed breakdown

trio

Name Old New Diff Outcome
for_constraint_pair_inner 605.64kB 2.49MB +320.84% (1.90MB) ⏫
for_constraint_pair_inner::interned_arguments 547.50kB 2.05MB +283.09% (1.51MB) ⏫
infer_expression_types_impl 5.66MB 6.10MB +7.82% (453.00kB) ⏫
when_constraint_set_assignable_to_owned_impl 684.23kB 1.02MB +52.94% (362.22kB) ⏫
TypePair 1.28MB 1.57MB +22.63% (296.81kB) ⏫
is_constraint_set_subtype_of_impl 68.21kB 202.19kB +196.42% (133.98kB) ⏫
when_constraint_set_equivalent_to_impl 60.62kB 150.09kB +147.61% (89.48kB) ⏫
InternedSequentConstraint 158.90kB 237.79kB +49.65% (78.89kB) ⏫
UnionType 338.52kB 384.84kB +13.69% (46.33kB) ⏫
Type<'db>::cached_materialization_ 83.42kB 126.49kB +51.63% (43.07kB) ⏫
union_from_two_elements 42.20kB 77.43kB +83.50% (35.23kB) ⏫
IntersectionType 284.92kB 255.87kB -10.20% (29.05kB) ⬇️
Type<'db>::cached_materialization_::interned_arguments 109.30kB 137.81kB +26.09% (28.52kB) ⏫
is_redundant_with_impl 359.34kB 377.41kB +5.03% (18.07kB) ⏫
VarianceSum 25.23kB 42.94kB +70.18% (17.71kB) ⏫
... 36 more

prefect

Name Old New Diff Outcome
infer_expression_types_impl 22.50MB 22.50MB +0.00% (568.00B) ⏫
Type<'db>::apply_specialization_inner_::interned_arguments 3.10MB 3.10MB +0.00% (160.00B) ⏫
Specialization 4.07MB 4.07MB +0.00% (128.00B) ⏫
Type<'db>::apply_specialization_inner_ 2.52MB 2.52MB +0.00% (120.00B) ⏫
GenericAlias 1.80MB 1.80MB +0.00% (72.00B) ⏫
infer_definition_types 36.72MB 36.72MB +0.00% (56.00B) ⏫
effective_superclass_variable_kind 155.25kB 155.26kB +0.01% (8.00B) ⏫
member_lookup_with_policy_inner 5.77MB 5.77MB +0.00% (8.00B) ⏫
Type<'db>::class_member_with_policy_inner_ 8.18MB 8.18MB +0.00% (8.00B) ⏫

Results for 4a8dbdc · Workflow run

@astral-automations-bot

Copy link
Copy Markdown
Contributor

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


Results for 4a8dbdc · Workflow run

This branch was successfully deployed

1 active deployment
automations — 4a8dbdc0 Deployed Nov 10, 2026 by sharkdp via security-review / security review #98023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug An issue describing something that isn't working, or a PR that fixes a bug ty The ty type checker

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant