Skip to content

Subclassing generic class using ParamSpec fails depending on how base class is referenced #14761

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
keosak opened this issue Feb 22, 2023 · 5 comments · Fixed by #15905
Closed
Labels
bug mypy got something wrong topic-paramspec PEP 612, ParamSpec, Concatenate

Comments

@keosak
Copy link

keosak commented Feb 22, 2023

Bug Report
Mypy reports an issue with subclassing generic class using ParamSpec, but only when the base class is referenced via module attribute access.

To Reproduce

File test1.py

from typing import Generic, ParamSpec

P = ParamSpec("P")

class Base(Generic[P]):
    def test(self, *args: P.args, **kwargs: P.kwargs):
        raise NotImplementedError

File test2_good.py

from test1 import Base

class Derived(Base[[int]]):
    def test(self, x: int):
        print(x)

File test2_bad.py

import test1

class Derived(test1.Base[[int]]):
    def test(self, x: int):
        print(x)

Expected Behavior
When running Mypy against the test2_good.py file, no issues are reported. This is what I expect.

(venv) debian@25081d5e7e53:/workspace$ mypy test2_good.py 
Success: no issues found in 1 source file

Actual Behavior
When running Mypy against the test2_bad.py file, an issue is reported. The only difference is how the base class is specified.

(venv) debian@25081d5e7e53:/workspace$ mypy test2_bad.py 
test2_bad.py:4: error: Bracketed expression "[...]" is not valid as a type  [valid-type]
test2_bad.py:4: note: Did you mean "List[...]"?
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.0.1 (compiled: yes)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.10.8 from the official Docker image
@keosak keosak added the bug mypy got something wrong label Feb 22, 2023
@keosak
Copy link
Author

keosak commented Mar 31, 2023

Still there as of Mypy 1.1.1.

@keosak
Copy link
Author

keosak commented Apr 11, 2023

Still there as of Mypy 1.2.0.

@keosak
Copy link
Author

keosak commented Jul 28, 2023

This is still happening in version 1.4.1. No reaction from anyone?

@AlexWaygood AlexWaygood added the topic-paramspec PEP 612, ParamSpec, Concatenate label Jul 28, 2023
ilevkivskyi added a commit that referenced this issue Aug 19, 2023
Fixes #14761
Fixes #15318
Fixes #14656
Fixes #13518

I noticed there is a bunch of inconsistencies in `semanal`/`typeanal`
for ParamSpecs, so I decided do a small cleanup. Using this opportunity
I also allow `Concatenate[int, ...]` (with literal Ellipsis), and reduce
verbosity of some errors.

cc @A5rocks
@hauntsaninja
Copy link
Collaborator

@keosak is the project you were running into this on open source?

@keosak
Copy link
Author

keosak commented Aug 20, 2023

Glad to see this fixed. I will test it with our code when the next version of Mypy is released.

@hauntsaninja No, it's not open source.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-paramspec PEP 612, ParamSpec, Concatenate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants