#๐Ÿ”’ TypeVar vs 3.12 syntax

6 messages ยท Page 1 of 1 (latest)

sterile swallow
#
  Argument corresponds to parameter "o" in function "__new__"PylancereportUnknownArgumentType```

This is the error i get when I use the PositiveValidator descriptor like this:

```py
class ABCValidator[T](metaclass=ABCMeta):
    def __init__(self, default: T | None = None):
        self._default = default

    def __set_name__(self, owner: Type, name: str):
        self.name = name
        self.private = "_" + name

    def __get__(self, obj: object, obj_type: Type[T]):
        return cast(T, getattr(obj, self.private, self._default))

    @abstractmethod
    def __set__(self, obj: object, value: T):
        setattr(obj, self.private, value)


class PositiveValidator[T: float | int](ABCValidator):
    def __set__(self, obj: object, value: T):
        if value < 0:
            raise ValueError(f"{self.name!r} must be a positive number")
        setattr(obj, self.private, value)

But when I modify the PositiveValidator, there's no longer a typing error from pylance.

type T = int | float

class PositiveValidator[T]:
    def __bytes__(self):
        return bytes(str(self.value), 'utf-8')

Who can explain what is the difference between the two versions?
And why does one work and the other doesn't?

sturdy bronzeBOT
#

@sterile swallow

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

sterile swallow
#

Actually i've noticed removing the ABCValidator inheritance is the key difference in this issue

sturdy bronzeBOT
#

@sterile swallow

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.

#

๐Ÿ”’ TypeVar vs 3.12 syntax