python-attrs / python-attrs/attrs

`attr.Attributes` incompatible with `Literal`

Open
#1,207 3 comments 0 reactions 1 assignee View on GitHub

@Tinche is already working on this.

Since Nov 25, 2023.

Typing
Dominant language
Python
Stars
5.8k
Forks
480
Avg merge
2h 15m
Merged PRs (30d)
2

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"

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.