GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — OrderedDict.fromkeys() returns Dict instead of OrderedDict · Issue #3800 · python/typeshed

OrderedDict.fromkeys() returns Dict instead of OrderedDict #3800

New issue
Copy link
New issue
Copy link
Closed
#6485
Closed
OrderedDict.fromkeys() returns Dict instead of OrderedDict#3800
#6485
Copy link

Description

@bluetech
bluetech
opened on Mar 1, 2020
Issue body actions

OrderedDict inherits fromkeys() from its parent Dict. dict.fromkeys has hardcoded return type dict:

typeshed/stdlib/2and3/builtins.pyi

Lines 999 to 1004 in 9500296

@staticmethod
@overload
def fromkeys(__iterable: Iterable[_T]) -> Dict[_T, Any]: ... # TODO: Actually a class method (mypy/issues#328)
@staticmethod
@overload
def fromkeys(__iterable: Iterable[_T], __value: _S) -> Dict[_T, _S]: ...

Easy way to fix this is to explicitly override to return OrderedDict, but this doesn't take care of the same problem in other dict subclasses.

I tried to fix it using generic self. For the first (short) form of fromkeys(), I think it should work:

class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
    _FK1 = TypeVar('_FK1', bound='dict[_KT, None]')

    @classmethod
    def fromkeys(cls: Type[_FK1], __iterable: Iterable[_T]) -> _FK1: ...

This uses the method described in the docs.

But the second (long) form has a complication - the value type of the dict depends on a generic argument to fromkeys. The following doesn't work:

class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
    _FK2 = TypeVar('_FK2', bound='dict[_KT, _S]')

    @classmethod
    def fromkeys(cls: Type[_FK2], __iterable: Iterable[_T], __value: _S) -> _FK2: ...

Understandably fails with Type variable "builtins._S" is unbound in the _FK2 = ... line.

I wonder if there is a way to make this work?

Reacted by Andrew

Activity

  1. JelleZijlstra commented on Mar 2, 2020

    @JelleZijlstra
    JelleZijlstra
    on Mar 2, 2020
    Member
    More actions

    Wow, I didn't know of that trick (defining a typevar within the class) to make the key type work. I can't think of a way to make the value type work correctly either.

    I think we should just add a stub for OrderedDict.fromkeys and not worry about other possible dict subclasses unless user reports come in that indicate that it's a common problem.

  2. bluetech commented on Mar 5, 2020

    @bluetech
    bluetech
    on Mar 5, 2020
    ContributorAuthor
    More actions

    I think we should just add a stub for OrderedDict.fromkeys and not worry about other possible dict subclasses unless user reports come in that indicate that it's a common problem.

    Sure. I'll do that after #3799 is settled to avoid a (semantic) conflict.

  3. ssbarnea commented on Jul 25, 2020

    @ssbarnea
    ssbarnea
    on Jul 25, 2020
    SponsorContributor
    More actions

    Is there a temporary workaround that we can use until this is sorted?

  4. AlexWaygood
    added a commit that references this issue on Dec 3, 2021

    Add `OrderedDict.fromkeys`

    This issue will close once commit c00a660 is merged into the 'main' branch.
    c00a660
  5. Akuli
    closed this as completedin #6485on Dec 3, 2021
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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.