Skip to content

[1.16 makes more common] Inference of Any against optional type infers Never #8829

Closed

Description

@rggjan

See the following example:

from typing import Any, Optional, TypeVar

T = TypeVar("T")


def assert_not_none(value: Optional[T]) -> T:
    assert value is not None
    return value


def foo1(a: Optional[Any]) -> int:
    return assert_not_none(a)[3]


def foo2(a: Optional[Any]) -> int:
    assert a is not None
    return a[3]

I would expect that foo1 and foo2 are equivalent. However, running mypy on this file, I get:

test.py:12: error: Value of type <nothing> is not indexable

For some reason, assert_not_none doesn't map Optional[Any] to Any, as it is supposed to (and which would accept the indexing), but instead maps it to <nothing> which throws an error. The second version with asserting that it is not None seems to work fine, though.

Activity

  1. msullivan commented on May 20, 2020

    @msullivan
    Collaborator

    Definitely a bug and a somewhat surprising one too. Probably an issue in constraints.py?

  2. ilevkivskyi commented on Feb 8, 2025

    @ilevkivskyi
    Member

    The root cause is:

    a: Any
    def assert_not_none(value: Optional[T]) -> T: ...
    reveal_type(assert_not_none(a))  # Revealed type is "Never"

    (and same with Optional[Any]) because of loose handling of unions in constraints.py. This is pretty bad IMO, so raising priority to high.

  3. changed the title [-]Value of type <nothing> is not indexable[/-] [+]Inference of Any against optional type infers Never[/+] on Feb 8, 2025
  4. JukkaL commented on Apr 30, 2025

    @JukkaL
    Collaborator

    I encountered this when preparing an internal codebase for the 1.16 release. The issue wasn't directly related to any change in 1.16, though -- some additional Any | None types are inferred after recent changes.

  5. changed the title [-]Inference of Any against optional type infers Never[/-] [+][1.16 makes more common] Inference of Any against optional type infers Never[/+] on May 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions