Skip to content

Non-overlapping identity check error for typing.get_origin #10405

Closed as not planned

Description

@eltoder

The following example produces an error in mypy --strict mode:

import typing

TypeSpec = typing.Any

def is_union(spec: TypeSpec) -> bool:
    return typing.get_origin(spec) is typing.Union

(it does work without the type alias).
This gives:

$ mypy --strict t.py
t.py:6: error: Non-overlapping identity check (left operand type: "type", right operand type: "<typing special form>")  [comparison-overlap]

It looks like get_origin() is declared to return type, but it can actually return some other things, notably Union and Literal.

mypy versions 1.4.1.

Activity

  1. JelleZijlstra commented on Jul 5, 2023

    @JelleZijlstra
    Member

    We already have a series of overloads for different cases, and the type return is only for GenericAlias arguments:

    if sys.version_info >= (3, 10):

    I think this might be better seen as a bug in mypy, or at least an unfortunate overload resolution.

  2. AlexWaygood commented on Jul 8, 2023

    @AlexWaygood
    Member

    or at least an unfortunate overload resolution.

    My (untested) guess would be that because TypeSpec is an alias for Any, any of the get_origin overloads are a valid match for the code snippet above. So mypy just blindly picks the first overload, and infers that the object returned by the get_origin call is an instance of ParamSpec.

    typeshed/stdlib/typing.pyi

    Lines 813 to 814 in 7646d38

    @overload
    def get_origin(tp: ParamSpecArgs | ParamSpecKwargs) -> ParamSpec: ...

    This is indeed slightly unfortunate, but I'm not sure what we can do about it.

  3. eltoder commented on Jul 9, 2023

    @eltoder
    ContributorAuthor

    Alright, opened a mypy bug: python/mypy#15630

  4. AlexWaygood commented on Jul 22, 2023

    @AlexWaygood
    Member

    Going to close this, as I don't think there's anything actionable for typeshed here; this is a mypy bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions