enthought / traits

Observable typed attributes for Python classes
Other
432 stars 85 forks source link

Dict type stubs are incorrect #1654

Closed mdickinson closed 2 years ago

mdickinson commented 2 years ago

The Dict type stubs (from https://github.com/enthought/traits/blob/f17231ebb166294d2d6d544424d52820f1979bff/traits-stubs/traits-stubs/trait_types.pyi#L444-L459) look like this:

class _Dict(_TraitType[_DictType[_S, _T], _DictType[_S, _T]]):
    def __init__(
            self,
            key_trait: _Union[
                _TraitType[_S, _T], _Type[_TraitType[_S, _T]]] = ...,
            value_trait: _Union[
                _TraitType[_S, _T], _Type[_TraitType[_S, _T]]] = ...,
            value: dict = ...,
            items: bool = ...,
            **metadata: _Any
    ) -> None:
        ...

class Dict(_Dict[_S, _T]):
    ...

That doesn't look right to me: from the _DictType[_S, _T] part (where _DictType is an alias for typing.Dict), the _S and _T metavariables should correspond to the key and value types, respectively. But then later we have:

key_trait: _Union[_TraitType[_S, _T], _Type[_TraitType[_S, _T]]] = ...,

So the definition of key_trait mentions the value type, and vice versa.

Also, for the "accepted" type we should probably be using Mapping rather than _DictType.