You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
additional issue in recently fixed pep-681 fix discovered #15020
hi, this is continuing from #14868, a user found another variant which points to something that likely needs to be adjusted in the fix just made in #15006.
same test case as #14868, but we add an additional alternative input type for the setter; the new logic does not interpret "optional" correctly (pyright works):
from __future__ importannotationsfromtypingimportAnyfromtypingimportGenericfromtypingimportOptionalfromtypingimportoverloadfromtypingimportTYPE_CHECKINGfromtypingimportTypeVarfromtypingimportUnionfromtyping_extensionsimportdataclass_transform_T=TypeVar("_T")
defmodel_field(
*,
default: Optional[Any] =None,
init: bool=True,
) ->Any:
raiseNotImplementedError()
@dataclass_transform(eq_default=True, order_default=True, field_specifiers=(model_field,))classModelBase:
def__init_subclass__(
cls,
*,
init: bool=True,
frozen: bool=False,
eq: bool=True,
order: bool=True,
):
...
classSpecialWrapper(Generic[_T]):
"""some special kind of type that is generic around a _T"""classMapped(Generic[_T]):
ifTYPE_CHECKING:
@overloaddef__get__(self, instance: None, owner: Any) ->Mapped[_T]:
...
@overloaddef__get__(self, instance: object, owner: Any) ->_T:
...
def__get__(
self, instance: Optional[object], owner: Any
) ->Union[Mapped[_T], _T]:
...
def__set__(self, instance: Any, value: Union[SpecialWrapper[_T], _T]) ->None:
"""the set method can accept values that are wrapped in the SpecialWrapper as well as the value directly. this is what causes the mypy issue. """
...
def__delete__(self, instance: Any) ->None:
...
classCustomer(ModelBase):
a: Mapped[Optional[str]] =model_field(default=None)
# worksc1=Customer(a='asdf')
# failsc2=Customer(a=None)
where the second case, Optional is not unwrapping the inner type correctly:
$ mypy test4.py
test4.py:77: error: Argument "a" to "Customer" has incompatible type "None"; expected "Union[SpecialWrapper[Optional[str]], str]" [arg-type]
Found 1 error in 1 file (checked 1 source file)
Activity
zzzeek commentedon Apr 7, 2023
for reference this is the SQLAlchemy case
Benjamin-T commentedon Jun 22, 2023
Any work being done on this?
cdce8p commentedon Jul 9, 2023
Opened #15628
Support optional for custom dataclass descriptors (#15628)