python / python/mypy

Union(Type) and TypeForm

Open
#21,910 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-runtime-semantics
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

My function should analyze types.
Both when --warn-unreachable is enabled, and when checking typing.Union, and when checking types.UnionType, an error occurs.

code:

from types import UnionType
from typing import Any, Union

from typing_extensions import TypeForm


def f(t: TypeForm[Any]) -> Any:
    if isinstance(t, Union):
        pass


def g(t: TypeForm[Any]) -> Any:
    if isinstance(t, UnionType):
        pass

errors:

main.py:8: error: Argument 2 to "isinstance" has incompatible type "<typing special form>"; expected "_ClassInfo"  [arg-type]
main.py:14: error: Statement is unreachable  [unreachable]
Found 2 errors in 1 file (checked 1 source file)

in runtime, the behavior of both isinstance is equivalent.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the example in main.py with mypy, with and without --warn-unreachable, and inspect how typing.Union and types.UnionType are handled when the value is annotated with typing_extensions.TypeForm. Done means the equivalent isinstance checks no longer produce the reported incompatible-type or unreachable-statement errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.