`attr.Attributes` incompatible with `Literal`
@Tinche is already working on this.
Since Nov 25, 2023.
Assessment
This issue has not been assessed yet.
Description
Because attr.Attributes causes the type it contains to be invariant, Literal types like Literal[1] to be incompatible with attr.Attributes[int]
from __future__ import annotations
from attr import Attribute
from attrs import define, field
def _check(self, attribute: Attribute[int], value: int):
print(type(attribute))
if value > 42:
msg = "x must be smaller or equal to 42"
raise ValueError(msg)
def trick_type_hinter() -> int:
return 1
a = trick_type_hinter()
@define
class LiteralDefault:
x: int = field(default=1, validator=_check)
@define
class LiteralFactory:
x: int = field(factory=lambda: 1, validator=_check)
@define
class CustomDefault:
x: int = field(default=a, validator=_check)
@define
class CustomFactory:
x: int = field(factory=lambda: a, validator=_check)
LiteralDefault error:
Type "(self: Unknown, attribute: Attribute[int], value: int) -> None" cannot be assigned to type "_ValidatorArgType[_T@field] | None"
Type "(self: Unknown, attribute: Attribute[int], value: int) -> None" cannot be assigned to type "(Any, Attribute[_T@field], _T@field) -> Any"
Parameter 2: type "Attribute[_T@field]" cannot be assigned to type "Attribute[int]"
"Attribute[Literal[1]]" is incompatible with "Attribute[int]"
Type parameter "_T@Attribute" is invariant, but "Literal[1]" is not the same as "int"
"function" is incompatible with "Sequence[(Any, Attribute[_T@field], _T@field) -> Any]"
Type cannot be assigned to type "None"
LiteralFactory error:
Type "(self: Unknown, attribute: Attribute[int], value: int) -> None" cannot be assigned to type "_ValidatorArgType[_T@field] | None"
Type "(self: Unknown, attribute: Attribute[int], value: int) -> None" cannot be assigned to type "(Any, Attribute[_T@field], _T@field) -> Any"
Parameter 2: type "Attribute[_T@field]" cannot be assigned to type "Attribute[int]"
"Attribute[Literal[1]]" is incompatible with "Attribute[int]"
Type parameter "_T@Attribute" is invariant, but "Literal[1]" is not the same as "int"
"function" is incompatible with "Sequence[(Any, Attribute[_T@field], _T@field) -> Any]"
Type cannot be assigned to type "None"
- Dominant language
- Python
- Stars
- 5.8k
- Forks
- 480
- Avg merge
- 2h 15m
- Merged PRs (30d)
- 2
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python-attrs/attrs
-
Difficulty 5/5 Over a week Newbie friendliness 20/100
python-attrs/attrs#1620 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 55/100
python-attrs/attrs#1596 · 2 comments · 1 reaction ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
python-attrs/attrs#1549 · 3 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 38/100
python-attrs/attrs#1543 · 2 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
python-attrs/attrs#1532 ·
All issues in python-attrs/attrs
Similar issues
-
documentation help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
simonw/sqlite-utils#872 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100