Skip to content

Specify that assert_type() uses equivalence #2011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
more lenient
  • Loading branch information
JelleZijlstra committed Jun 29, 2025
commit ac0a0b4b7d13cf04145f2d0fca9b34afe30996b8
9 changes: 8 additions & 1 deletion conformance/tests/directives_assert_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ def func1(
e: Annotated[Literal[4], ""],
):
assert_type(a, int | str) # OK
assert_type(a, int | bool | str) # OK (equivalent type)
assert_type(b, list[int]) # OK
assert_type(c, Any) # OK
assert_type(d, "ForwardReference") # OK
assert_type(e, Literal[4]) # OK

assert_type(a, int) # E: Type mismatch
assert_type(a, Any) # E: Type mismatch
assert_type(c, int) # E: Type mismatch
assert_type(e, int) # E: Type mismatch

Expand All @@ -34,5 +34,12 @@ def func1(
assert_type(a, int | str, a) # E: too many arguments


# > If the two types are :term:`equivalent` but syntactically different,
# > the type checker may reject the ``assert_type()`` call::

def func2(name: str):
assert_type(name, str | Literal["spam"]) # E?: Equivalent but not identical


class ForwardReference:
pass
12 changes: 11 additions & 1 deletion docs/spec/directives.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,19 @@ should emit an error if the value is not of the specified type::

def greet(name: str) -> None:
assert_type(name, str) # OK, inferred type of `name` is `str`
assert_type(name, str | Literal["spam"]) # OK, this type is equivalent to `str`
assert_type(name, int) # type checker error

If the two types are :term:`equivalent` but syntactically different,
the type checker may reject the ``assert_type()`` call::

from typing import assert_type, Literal

def greet(name: str) -> None:
assert_type(name, str | Literal["spam"]) # type checker may error

Type checkers should aim to minimize cases where they reject
``assert_type()`` calls that use equivalent types.

The second argument must be a valid :term:`type expression`.

.. _`reveal-type`:
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy