Skip to content

TypeError when running example recipe under "Runtime use of types" in the documentation #531

Closed
@DavidOGrady

Description

@DavidOGrady

Hello,

I believe I have found a potential issue in either typing-extensions or the documentation for typing-extensions when running the example recipe code under "Runtime use of types" in the documentation. Link to documentation: https://typing-extensions.readthedocs.io/en/latest/#runtime-use-of-types

Im have created the following script to test out typing-extensions on my wsl environment.

import functools
import typing
import typing_extensions
from typing import Tuple, Any
from functools import partial
from typing_extensions import get_origin

# Use an unbounded cache for this function, for optimal performance
@functools.lru_cache(maxsize=None)
def get_typing_objects_by_name_of(name: str) -> Tuple[Any, ...]:
    result = tuple(
        getattr(module, name)
        # You could potentially also include mypy_extensions here,
        # if your library supports mypy_extensions
        for module in (typing, typing_extensions)
        if hasattr(module, name)
    )
    if not result:
        raise ValueError(
            f"Neither typing nor typing_extensions has an object called {name!r}"
        )
    return result


# Use a cache here as well, but make it a bounded cache
# (the default cache size is 128)
@functools.lru_cache()
def is_typing_name(obj: object, name: str) -> bool:
    return any(obj is thing for thing in get_typing_objects_by_name_of(name))


if __name__ == "__main__":
    print("Hello world")
    is_literal = partial(is_typing_name, name="Literal")
    print(is_literal(typing.Literal))
    print(is_literal(typing_extensions.Literal))
    print(is_literal(typing.Any))
    print(is_literal(get_origin(typing.Literal[42])))
    print(is_literal(get_origin(typing_extensions.Final[42])))

However, when i run this code (after using pip to install typing-extensions 4.12.2 and using python version 3.8.10), I get the following error:

test-user@test-vm:~$ cd temp-venv/
test-user@test-vm:~/temp-venv$ source bin/activate
(temp-venv) test-user@test-vm:~/temp-venv$ python
Python 3.8.10 (default, Jan 17 2025, 14:40:23)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
(temp-venv) test-user@test-vm:~/temp-venv$ ls
bin  include  lib  lib64  pyvenv.cfg  share
(temp-venv) test-user@test-vm:~/temp-venv$ cd bin
(temp-venv) test-user@test-vm:~/temp-venv/bin$ ls
Activate.ps1  activate.csh   easy_install      pip   pip3.8  python3
activate      activate.fish  easy_install-3.8  pip3  python  test-typing-example.py
(temp-venv) test-user@test-vm:~/temp-venv/bin$ pip list
Package           Version
----------------- -------
pip               20.0.2
pkg-resources     0.0.0
setuptools        44.0.0
typing-extensions 4.12.2
(temp-venv) test-user@test-vm:~/temp-venv/bin$ python3 test-typing-example.py
Hello world
True
True
False
True
Traceback (most recent call last):
  File "test-typing-example.py", line 39, in <module>
    print(is_literal(get_origin(typing_extensions.Final[42])))
  File "/usr/lib/python3.8/typing.py", line 261, in inner
    return func(*args, **kwds)
  File "/usr/lib/python3.8/typing.py", line 350, in __getitem__
    item = _type_check(parameters, f'{self._name} accepts only single type.')
  File "/usr/lib/python3.8/typing.py", line 149, in _type_check
    raise TypeError(f"{msg} Got {arg!r:.100}.")
TypeError: Final accepts only single type. Got 42.

Out of interest, I ran the same test file on clean Python docker images (with the latest tag which had Python 3.13.2 and an older image that had Python 3.9.21, i also manually installed typing-extensions on both docker containers using pip install typing-extensions). The image that had Python 3.13.2 installed did not show any issues and print False as expected. However, the docker image that had Python 3.9.21 installed on it also showed the same TypeError. Out of interest, I replaced typing_extensions.Final with typing.Final the same error was seen.

Also, if I replaced the 42 with the word int and the TypeError was not seen.

I don't suppose this is a typo in the documentation or are there specific versions of Python 3.8 and 3.9 that don't work with typing-extensions 4.12.2?

Any help or advice would be appreciated.
Thanks,

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions