Skip to content

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

Closed

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__

Activity

  1. changed the title [-]dict() should anything with .keys() and __getitem__[/-] [+]dict() should support anything with .keys() and __getitem__[/+] on Aug 7, 2020
  2. srittau commented on Aug 7, 2020

    @srittau
    Collaborator

    Sounds reasonable. See also #3576 and #4325.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions