Skip to content

dict() should support anything with .keys() and __getitem__ #4409

Closed
@Akuli

Description

@Akuli
from typing import List
class Foo:
    def keys(self) -> List[str]:
        return ['a', 'b', 'c']
    def __getitem__(self, key: str) -> str:
        return f'value of {key}'
print(dict(Foo()))   # {'a': 'value of a', 'b': 'value of b', 'c': 'value of c'}

works at runtime but fails type check

tkinter widgets are like this, they aren't Mapping but they have .keys() and __getitem__

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions