[Python] Unbound TypeVar variable in overloaded class

Discussão em 'Python' iniciado por Stack, Julho 7, 2025.

  1. Stack

    Stack Membro Participativo

    I have this following code which is a simplified version of an entity component system in python:

    from __future__ import annotations

    from typing import TYPE_CHECKING, TypeVar, overload

    if TYPE_CHECKING:
    from collections.abc import Generator


    T = TypeVar("T")
    T1 = TypeVar("T1")
    T2 = TypeVar("T2")


    class Registry:
    def __init__(self) -> None:
    self._next_game_object_id = 0
    self._components: dict[type[T], set[int]] = {}
    self._game_objects: dict[int, dict[type[T], T]] = {}

    @overload
    def get_components(
    self,
    component: type[T],
    ) -> Generator[tuple[int, T], None, None]:
    ...

    @overload
    def get_components(
    self,
    component: type[T],
    component_two: type[T1],
    ) -> Generator[tuple[int, T, T1], None, None]:
    ...

    @overload
    def get_components(
    self,
    component: type[T],
    component_two: type[T1],
    component_three: type[T2],
    ) -> Generator[tuple[int, T, T1, T2], None, None]:
    ...

    def get_components(self, *components: type[T]) -> Generator[tuple[int, tuple[T, ...]], None, None]:
    game_object_ids = set.intersection(
    *(self._components[component] for component in components),
    )

    for game_object_id in game_object_ids:
    yield game_object_id, tuple(
    self._game_objects[game_object_id][component]
    for component in components
    )


    However, I'm getting some mypy errors which I cannot figure out. One of which says that the TypeVar T is unbound and another says that get_components does not accept all possible arguments of signature 1, 2, and 3. How can I fix these errors?

    Continue reading...

Compartilhe esta Página