Skip to content

Conversation

@Glyphack
Copy link
Contributor

@Glyphack Glyphack commented Dec 15, 2024

Summary

Resolves #14988

Display union of Literals like other type checkers do.

With this change we lose the sorting behavior. And we show the types as they appeared. So it's deterministic and tests should not be flaky.
This is similar to how Mypy reveals the type.

In some cases this makes it harder to know what is the order in revealed type when writing tests but since it's consistent after the test fails we know the order.

Test Plan

I adjusted mdtests for this change. Basically merged the int and string types of the unions.

In cases where we have types other than numbers and strings like this one. We only group the strings and numbers as the issue suggsted.

def _(flag: bool, flag2: bool):
    if flag:
        f = 1
    elif flag2:
        f = "foo"
    else:
        def f() -> int:
            return 1
    # error: "Object of type `Literal[1, "foo", f]` is not callable (due to union elements Literal[1], Literal["foo"])"
    # revealed: Unknown | int
    reveal_type(f())

pyright example

mypy example

@Glyphack Glyphack marked this pull request as draft December 15, 2024 20:31
@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Dec 15, 2024
@github-actions
Copy link
Contributor

github-actions bot commented Jan 5, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@Glyphack Glyphack marked this pull request as ready for review January 5, 2025 16:10
Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

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

This looks good to me! Will delete the contentious comment and then merge :) Thank you !!

@carljm carljm enabled auto-merge (squash) January 8, 2025 00:53
@carljm carljm merged commit 03ff883 into astral-sh:main Jan 8, 2025
20 checks passed
dcreager added a commit that referenced this pull request Jan 8, 2025
* main:
  [`pylint`] Fix `unreachable` infinite loop (`PLW0101`) (#15278)
  fix invalid syntax in workflow file (#15357)
  [`pycodestyle`] Avoid false positives related to type aliases (`E252`) (#15356)
  [`flake8-builtins`] Disapply `A005` to stub files (#15350)
  Improve logging system using `logLevel`, avoid trace value (#15232)
  [`flake8-builtins`] Rename `A005` and improve its error message (#15348)
  Spruce up docs for pydoclint rules (#15325)
  [`flake8-type-checking`] Apply `TC008` more eagerly in `TYPE_CHECKING` blocks and disapply it in stubs (#15180)
  [red-knot] `knot_extensions` Python API (#15103)
  Display Union of Literals as a Literal (#14993)
  [red-knot] all types are assignable to object (#15332)
  [`ruff`] Parenthesize arguments to `int` when removing `int` would change semantics in `unnecessary-cast-to-int` (`RUF046`) (#15277)
  [`eradicate`] Correctly handle metadata blocks directly followed by normal blocks (`ERA001`) (#15330)
  Narrowing for class patterns in match statements (#15223)
  [red-knot] add call checking (#15200)
  Spruce up docs for `slice-to-remove-prefix-or-suffix` (`FURB188`) (#15328)
  [`internal`] Return statements in finally block point to end block for `unreachable` (`PLW0101`) (#15276)
  [`ruff`] Treat `)` as a regex metacharacter (`RUF043`, `RUF055`) (#15318)
  Use uv consistently throughout the documentation (#15302)
@Glyphack Glyphack deleted the literal-union branch May 21, 2025 20:26
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.

[red-knot] fully consolidate display of heterogenous unions of literals

5 participants