Skip to content

[ty] Fix hover showing Unknown for bare Final instance attributes#23003

Merged
sharkdp merged 2 commits intomainfrom
charlie/hov
Feb 3, 2026
Merged

[ty] Fix hover showing Unknown for bare Final instance attributes#23003
sharkdp merged 2 commits intomainfrom
charlie/hov

Conversation

@charliermarsh
Copy link
Member

@charliermarsh charliermarsh commented Jan 31, 2026

Summary

For non-name annotated assignment targets like self.x: Final = value, the expression type stored for self.x was the annotation's inner type (Unknown, for Final).

The name-target path (x: Final = value) already stores the inferred RHS type instead, so we now do the same for attributes.

Closes astral-sh/ty#2509.

@charliermarsh charliermarsh added server Related to the LSP server ty Multi-file analysis & type inference labels Jan 31, 2026
@astral-sh-bot
Copy link

astral-sh-bot bot commented Jan 31, 2026

Typing conformance results

No changes detected ✅

@astral-sh-bot
Copy link

astral-sh-bot bot commented Jan 31, 2026

mypy_primer results

Changes were detected when running on open source projects
scikit-build-core (https://github.com/scikit-build/scikit-build-core)
- src/scikit_build_core/build/wheel.py:99:20: error[no-matching-overload] No overload of bound method `__init__` matches arguments
- Found 47 diagnostics
+ Found 46 diagnostics

rotki (https://github.com/rotki/rotki)
+ rotkehlchen/chain/decoding/tools.py:96:44: warning[unused-type-ignore-comment] Unused blanket `type: ignore` directive
- rotkehlchen/chain/decoding/tools.py:97:13: error[invalid-argument-type] Argument to function `decode_transfer_direction` is incorrect: Expected `BTCAddress | ChecksumAddress | SubstrateAddress | SolanaAddress`, found `A@BaseDecoderTools`
+ rotkehlchen/chain/decoding/tools.py:99:13: error[invalid-argument-type] Argument to function `decode_transfer_direction` is incorrect: Expected `Sequence[A@BaseDecoderTools]`, found `Unknown | tuple[BTCAddress, ...] | tuple[ChecksumAddress, ...] | tuple[SubstrateAddress, ...] | tuple[SolanaAddress, ...]`
- rotkehlchen/chain/decoding/tools.py:98:13: error[invalid-argument-type] Argument to function `decode_transfer_direction` is incorrect: Expected `BTCAddress | ChecksumAddress | SubstrateAddress | SolanaAddress | None`, found `A@BaseDecoderTools | None`
+ rotkehlchen/chain/decoding/tools.py:100:62: warning[unused-type-ignore-comment] Unused blanket `type: ignore` directive
- Found 2054 diagnostics
+ Found 2055 diagnostics

core (https://github.com/home-assistant/core)
- homeassistant/util/variance.py:47:12: error[invalid-return-type] Return type does not match returned value: expected `(**_P@ignore_variance) -> _R@ignore_variance`, found `_Wrapped[_P@ignore_variance, int | _R@ignore_variance | float | datetime, _P@ignore_variance, _R@ignore_variance | int | float | datetime]`
- Found 14513 diagnostics
+ Found 14512 diagnostics

No memory usage changes detected ✅

@charliermarsh
Copy link
Member Author

I assume the behavior here is intentionally different from how name assignments are handled? This PR makes them more consistent, but I'm likely overlooking the reason that they diverge today.

@charliermarsh charliermarsh marked this pull request as ready for review January 31, 2026 20:31
Copy link
Contributor

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

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

Thank you, this seems very reasonable to me.

I assume the behavior here is intentionally different from how name assignments are handled? This PR makes them more consistent, but I'm likely overlooking the reason that they diverge today.

I do not recall any intentional divergence of behavior. A reveal_type(self.a) immediately following the self.a: <type> = <rhs-expr> annotated attribute assignment would also show the RHS type (due to attribute assignment narrowing). So I think this makes sense.

Note that this will not just affect bare Final annotations, but also something like self.a: int = 1 where we previously revealed int instead of Literal[1].

@sharkdp sharkdp merged commit a57cf57 into main Feb 3, 2026
49 checks passed
@sharkdp sharkdp deleted the charlie/hov branch February 3, 2026 14:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

server Related to the LSP server ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Annotating an instance attribute with Final means no type is shown when hovering over it

2 participants