8000 mypy doesn't support typing.OrderedDict · Issue #6904 · python/mypy · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

mypy doesn't support typing.OrderedDict #6904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
csingley opened this issue May 26, 2019 · 9 comments
Closed

mypy doesn't support typing.OrderedDict #6904

csingley opened this issue May 26, 2019 · 9 comments

Comments

@csingley
Copy link

What to do about this state of affairs?

? python -V && python -c "from typing import OrderedDict"                                                                         
Python 3.7.3
? mypy -V && mypy -c "from typing import OrderedDict"
mypy 0.710+dev.5f08ccf029aa3046b15e1afc60743ba692e4758d
<string>:1: error: Module 'typing' has no attribute 'OrderedDict'
@ilevkivskyi
Copy link
Member

x: int = 'hi' also doesn't give any errors at runtime, so this is not an argument, import OrderedDict from collections, not from other places where it was imported for internal use.

@csingley
Copy link
Author

How can we provide type annotations for OrderedDict keys/values?

@ilevkivskyi
Copy link
8000
Member

You have two options:

Either use:

from __future__ import annotations
from collections import OrderedDict
def order(x) -> OrderedDict[str, int]: ...

but it works only on Python 3.7+

Or use:

from collections import OrderedDict
def order(x) -> 'OrderedDict[str, int]': ...

it works on all Python 3 versions but a bit ugly.

Also it is all in the docs, read them. For example https://mypy.readthedocs.io/en/latest/common_issues.html#using-classes-that-are-generic-in-stubs-but-not-at-runtime

@csingley
Copy link
Author

Thank you.

@roddycx
Copy link
roddycx commented Aug 5, 2019

x: int = 'hi' also doesn't give any errors at runtime, so this is not an argument, import OrderedDict from collections, not from other places where it was imported for internal use.

I don't think this is right. As of Python 3.7.2, typing includes a copy of collections.OrderedDict that is listed in the docs as "A generic version of collections.OrderedDict". This behaves the same as collections.OrderedDict at runtime, but additionally can be subscripted so type annotations do work.

In other words, it is a class that is generic both in stubs and at runtime. So the fact that mypy doesn't work correctly with it for Python 3.7.2+ is an issue.

@roddycx
Copy link
roddycx commented Aug 5, 2019

More specifically, the issue is that mypy isn't using the latest version of typeshed which does have a stub for OrderedDict.

@JelleZijlstra
Copy link
Member

The next release of mypy will include more recent changes to typeshed.

@seoester
Copy link
seoester commented Nov 18, 2019

It is still not possible to use typing.OrderedDict with mypy 0.740.

# mypy -c 'from typing import OrderedDict; o: OrderedDict[str, str] = OrderedDict()'
<string>:1: error: Variable "typing.OrderedDict" is not valid as a type
<string>:1: error: "TypeAlias" not callable
Found 2 errors in 1 file (checked 1 source file)

collections.OrderedDict works, however.

# mypy -c 'from collections import OrderedDict; o: OrderedDict[str, str] = OrderedDict()'
Success: no issues found in 1 source file

@logicplace
Copy link
logicplace commented May 27, 2020

Because this is still not fixed (as of master, even) and I had to dive into a really ugly workaround, figured I'd share it for fellow Googlers.

Context: I wanted to subclass OrderedDict with a specific key type and a to-be-defined-later value type so as to share common methods between two classes with different value types. I also wanted it to actually work in VS Code properly. This is how to make that work for the current version of mypy:

from typing import TYPE_CHECKING, TypeVar

T_co = TypeVar("T_co", covariant=True)

# Workaround
from collections import OrderedDict
if not TYPE_CHECKING:
	class OrderedDict(OrderedDict):
		def __class_getitem__(cls, types):
			return cls
# /Workaround

class Struct(OrderedDict[str, T_co]):
	pass

test = Struct["str"]()
tt = test.get("a", "b")
print(tt)
$ mypy test.py
Success: no issues found in 1 source file

$ python test.py 
b

VS Code:
image

mristin added a commit to aas-core-works/abnf-to-regexp that referenced this issue May 29, 2021
Python 3.6 does not support `typing.OrderedDict`.

See python/mypy#6904.
mristin added a commit to aas-core-works/abnf-to-regexp that referenced this issue May 29, 2021
Python 3.6 does not support `typing.OrderedDict`.

See python/mypy#6904.
mristin added a commit to aas-core-works/abnf-to-regexp that referenced this issue May 29, 2021
Python 3.6 does not support `typing.OrderedDict`.

See python/mypy#6904.
malfet added a commit to pytorch/pytorch that referenced this issue Feb 8, 2022
Followup after #72211 - as python/mypy#6904 (comment) in Python-3.7+ world OrderedDict from collections can be used directly for type annotations
malfet added a commit to pytorch/pytorch that referenced this issue Feb 9, 2022
Followup after #72211 - as python/mypy#6904 (comment) in Python-3.7+ world OrderedDict from collections can be used directly for type annotations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants
0