Skip to content
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

[Add test case] Bug in return type of deque.copy() in generic type #12641

Closed
NiklasRosenstein opened this issue Apr 21, 2022 · 3 comments · Fixed by #12648
Closed

[Add test case] Bug in return type of deque.copy() in generic type #12641

NiklasRosenstein opened this issue Apr 21, 2022 · 3 comments · Fixed by #12648
Labels

Comments

@NiklasRosenstein
Copy link

NiklasRosenstein commented Apr 21, 2022

Bug Report

Mypy reasons incorrectly about the return type of collections.deque.copy() if the item type is a generic with a type parameter inside a generic class.

To Reproduce

from collections import deque
from typing import Any, Callable, Generic, List, Protocol, TypeVar

T = TypeVar("T")

def test(item_type: T) -> None:
    a: deque[List[T]] = deque()
    b: deque[List[T]] = a.copy()  # ok


class Test(Generic[T]):
    def test(self) -> None:
        a: deque[List[T]] = deque()
        b: deque[List[T]] = a.copy()  # error: Incompatible types in assignment (expression has type "deque[List[List[T]]]", variable has type "deque[List[T]]")  [assignment]

Expected Behavior

The return type in the above example should be List[T] not List[List[T]].

Your Environment

  • Mypy version used: 0.942

  • Mypy command-line flags: mypy test.py

  • Mypy configuration options from mypy.ini (and other config files):

    [tool.mypy]
    strict = true
    namespace_packages = true
    ignore_missing_imports = true
    show_error_codes = true
    exclude = "tests"
  • Python version used: 3.10.2

  • Operating system and version: OSX 12.2.1 Monterey

@NiklasRosenstein NiklasRosenstein added the bug mypy got something wrong label Apr 21, 2022
@NiklasRosenstein NiklasRosenstein changed the title Bug in return type of deque.copy() if type variables are involved Bug in return type of deque.copy() if generic type Apr 21, 2022
@NiklasRosenstein NiklasRosenstein changed the title Bug in return type of deque.copy() if generic type Bug in return type of deque.copy() in generic type Apr 21, 2022
@JelleZijlstra
Copy link
Member

deque.copy in typeshed is just def copy(self: Self) -> Self:, so I do think this is a bug in mypy. Does it reproduce on current master with #12590 applied?

@AlexWaygood
Copy link
Member

deque.copy in typeshed is just def copy(self: Self) -> Self:, so I do think this is a bug in mypy. Does it reproduce on current master with #12590 applied?

I can reproduce with 0.942, but not with mypy master. The "first good commit" is indeed 20b0b9b -- so yep, #12590 fixed this.

@JelleZijlstra
Copy link
Member

Great, let's add a test case for this one too.

@JelleZijlstra JelleZijlstra changed the title Bug in return type of deque.copy() in generic type [Add test case] Bug in return type of deque.copy() in generic type Apr 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants