-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Closed
Labels
Description
Bug Report
Using a frozen dataclass with an attribute typed as a TypeAlias
for a Callable
results in mypy treating the attribute as the return value. See reproduce for more easy to understand example.
To Reproduce
from dataclasses import dataclass
from typing import Callable, TypeAlias
Func: TypeAlias = Callable[..., None]
@dataclass(frozen=True, eq=False)
class Foo:
bar: Func
def example() -> None:
pass
Foo(bar=example).bar()
results in
test.py:13:1: error: "None" not callable [misc]
Found 1 error in 1 file (checked 1 source file)
Expected Behavior
mypy should pass
Actual Behavior
Removing frozen=True
causes mypy to pass this code.
Your Environment
- Mypy version used: 0.931
- Mypy command-line flags: N/A
- Mypy configuration options from
mypy.ini
(and other config files):
python_version=3.10
platform=linux
plugins=pydantic.mypy, sqlalchemy.ext.mypy.plugin
show_column_numbers=True
show_error_codes=True
# show error messages from unrelated files
follow_imports=normal
# show errors about unsatisfied imports
ignore_missing_imports = False
# be strict
disallow_untyped_calls=True
warn_return_any=True
strict_optional=True
warn_no_return=True
warn_redundant_casts=True
warn_unused_ignores=True
# The following are off by default. Flip them on if you feel
# adventurous.
disallow_untyped_defs=True
check_untyped_defs=True
- Python version used: 3.10.2
- Operating system and version: Ubuntu 20.04
Metadata
Metadata
Assignees
Labels
Projects
Milestone
Relationships
Development
Select code repository
Activity
uSpike commentedon Mar 19, 2022
I have a test case that reproduces this:
uSpike commentedon Mar 19, 2022
Upon further investigation, this issue is not specific to
TypeAlias
. Anydataclass
with aCallable
attribute will not work if frozen. See this failing test case:Here's a simple fix that i will make a PR for shortly:
It seems that the dataclass plugin was not calling
self._propertize_callables()
if the dataclass is frozen. I'll look more into the history around that code to see if there's a reason why it was written that way, but the above change passes the entire test suite.[-]Frozen dataclass Callable attribute cannot be a TypeAlias[/-][+]Callable types are not handled correctly in frozen dataclasses[/+]wrobell commentedon Mar 25, 2022
Looks like regression #10711?
uSpike commentedon Mar 25, 2022
I'm not sure it's really a regression since mypy seems to have never properly supported frozen dataclasses with Callable types.