Skip to content

[ty] Avoid order-dependent inference for guarded attributes - #27550

Draft
charliermarsh wants to merge 17 commits into
charlie/fix-present-key-todosfrom
charlie/fix-4076-deferred-attribute-presence
Draft

[ty] Avoid order-dependent inference for guarded attributes#27550
charliermarsh wants to merge 17 commits into
charlie/fix-present-key-todosfrom
charlie/fix-4076-deferred-attribute-presence

Conversation

@charliermarsh

Copy link
Copy Markdown
Member

Summary

We infer implicit instance attributes from assignments such as:

class C:
    def __init__(self):
        if not hasattr(self, "x"):
            self.x = self.__str__

A negative hasattr guard normally narrows self to Self & ~Protocol[x]. When the same guard initializes x, simplifying that type requires resolving x again, creating a Salsa cycle whose recovered result depends on file-checking order rather than constraint-set ordering.

Avoid the cycle by leaving self unnarrowed only when existing use-def reachability proves that the same standalone guard initializes the same implicit instance attribute in the corresponding branch. Preserve normal narrowing for class-backed attributes, previously initialized attributes, compound conditions, and assignments in the opposite branch.

Stacked on #25645.

Closes astral-sh/ty#4076.

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

astral-sh-bot Bot commented Aug 6, 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 96.96%. The percentage of expected errors that received a diagnostic held steady at 92.96%. The number of fully passing files held steady at 106/133.

@astral-sh-bot

astral-sh-bot Bot commented Aug 6, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Aug 6, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

Lint rule Added Removed Changed
unused-type-ignore-comment 0 2 0
unresolved-attribute 0 1 0
Total 0 3 0

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

Raw diff:

websockets (https://github.com/aaugustin/websockets)
- src/websockets/trio/client.py:543:50 error[unresolved-attribute] Object of type `Self@__aenter__` has no attribute `nursery`

werkzeug (https://github.com/pallets/werkzeug)
- src/werkzeug/debug/__init__.py:315:55 warning[unused-type-ignore-comment] Unused blanket `type: ignore` directive
- src/werkzeug/debug/__init__.py:331:55 warning[unused-type-ignore-comment] Unused blanket `type: ignore` directive

Full report with detailed diff (timing results)

@charliermarsh
charliermarsh force-pushed the charlie/fix-present-key-todos branch from 71eb809 to 8b42130 Compare August 6, 2026 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant