Skip to content

Conversation

@Gankra
Copy link
Contributor

@Gankra Gankra commented Nov 10, 2025

I'm not 100% sold on this implementation, but it's a strict improvement and it adds a ton of snapshot tests for future iteration.

Part of astral-sh/ty#494

@Gankra Gankra added server Related to the LSP server ty Multi-file analysis & type inference labels Nov 10, 2025
Comment on lines +460 to +461
x = 1
y[: Literal[1]] = x
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This case is why this approach is mediocre. The type-directed approach would catch this trivially.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is actually so bad? It tells the user that we inferred the type of y as Literal[1], but mypy would infer it as int here -- that's potentially useful information. The y = x assignment could also happen much lower down in the function, far away from the initial x = 1 assignment

@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 10, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 10, 2025

mypy_primer results

No ecosystem changes detected ✅

No memory usage changes detected ✅

Comment on lines +460 to +461
x = 1
y[: Literal[1]] = x
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is actually so bad? It tells the user that we inferred the type of y as Literal[1], but mypy would infer it as int here -- that's potentially useful information. The y = x assignment could also happen much lower down in the function, far away from the initial x = 1 assignment

return x
x1, y1 = (1, 'abc')
x2[: Literal[1]], y2[: Literal["abc"]] = (x1, y1)
Copy link
Member

Choose a reason for hiding this comment

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

this, on the other hand, feels kinda yucky to me 😆

@Gankra Gankra enabled auto-merge (squash) November 10, 2025 19:41
@Gankra Gankra merged commit d258302 into main Nov 10, 2025
40 checks passed
@Gankra Gankra deleted the gankra/ctor-hints branch November 10, 2025 19:51
Gankra added a commit that referenced this pull request Nov 10, 2025
It's everyone's favourite language corner case!

Also having kicked the tires on it, I'm pretty happy to call this (in
conjunction with #21367):

Fixes astral-sh/ty#494

There's cases where you can make noisy Literal hints appear, so we can
always iterate on it, but this handles like, 98% of the cases in the
wild, which is great.

---------

Co-authored-by: David Peter <sharkdp@users.noreply.github.com>
dcreager added a commit that referenced this pull request Nov 11, 2025
* origin/main: (38 commits)
  [ty] Make implicit submodule imports only occur in global scope (#21370)
  [ty] introduce local variables for `from` imports of submodules in `__init__.py(i)` (#21173)
  [`ruff`] Ignore `str()` when not used for simple conversion (`RUF065`) (#21330)
  [ty] implement `typing.NewType` by adding `Type::NewTypeInstance`
  [ty] supress inlay hints for `+1` and `-1` (#21368)
  [ty] Use type context for inference of generic constructors (#20933)
  [ty] Improve generic call expression inference (#21210)
  [ty] supress some trivial expr inlay hints (#21367)
  [`configuration`] Fix unclear error messages for line-length values exceeding `u16::MAX` (#21329)
  [ty] Fix incorrect inference of `enum.auto()` for enums with non-`int` mixins, and imprecise inference of `enum.auto()` for single-member enums (#20541)
  [`refurb`] Detect empty f-strings (`FURB105`) (#21348)
  [ty] provide `import` completion when in `from <name> <name>` statement (#21291)
  [ty] elide redundant inlay hints for function args (#21365)
  Fix syntax error false positive on alternative `match` patterns (#21362)
  Add a new "Opening a PR" section to the contribution guide (#21298)
  [`flake8-simplify`] Fix SIM222 false positive for `tuple(generator) or None` (`SIM222`) (#21187)
  Rebuild ruff binary instead of sharing it across jobs (#21361)
  [ty] Fix `--exclude` and `src.exclude` merging (#21341)
  [ty] Add support for properties that return `Self` (#21335)
  Add upstream linter URL to `ruff linter --output-format=json` (#21316)
  ...
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.

3 participants