- Notifications
You must be signed in to change notification settings - Fork263
-
Hi All, Is this a bug in mypy? As best I (and a bunch of unit tests!) can tell, this code is good: fromdatetimeimportdatetimedefadd(cls:type[datetime],instance:datetime|int|None=None,/,*args:int,**kw:int,)->datetime:ifisinstance(instance,datetime):passelifisinstance(instance,int):instance=cls(instance,*args,**kw)else:instance=cls(*args,**kw)returninstance ...but mypy (1.15.0 runnong on Python 3.12.1) complains with:
|
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 2 comments 2 replies
-
Mypy is accurately telling you that the datetime constructor has an argument ( I would say it feels slightly arbitrary what the type checker chooses to complain about here, in the sense that this code is much more broadly type-unsafe than that: any invalid keyword name provided to this wrapper, or too many positional arguments, will also result in a This might be a good use case for |
BetaWas this translation helpful?Give feedback.
All reactions
-
I'd argue about accurately, or certainly "helpfully", as this specific error message didn't communicate to what your text cleared up:
This would have been easier for me to understanding:
I guess we're also running into limitations of *args typing: there's no way to say "this may only be up to this length". What's annoying is that even with added overload definitions, mypy still doesn't infer that this problem can't be hit: fromdatetimeimportdatetime,tzinfofromtypingimportoverload@overloaddefadd(cls:type[datetime],year:int,month:int,day:int,hour:int=0,minute:int=0,second:int=0,microsecond:int=0,tzinfo:tzinfo|None=None,)->datetime: ...@overloaddefadd(cls:type[datetime],instance:datetime,/,)->datetime: ...defadd(cls:type[datetime],instance:datetime|int|None=None,/,*args:int,tzinfo:tzinfo|None=None,**kw:int,)->datetime:ifisinstance(instance,datetime):passelifisinstance(instance,int):instance=cls(instance,*args,tzinfo=tzinfo,**kw)else:instance=cls(*args,tzinfo=tzinfo,**kw)returninstance ...in fact, more errors pop up :-(
If you could show me how to use |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Quibbles with mypy error message wording should go to the mypy bug tracker, not here. This is the closest I can get with fromdatetimeimportdatetimefromtypingimportCallabledefadd[**P](cls:Callable[P,datetime],instance:datetime|int|None=None,/,*args:P.args,**kw:P.kwargs,)->datetime:ifisinstance(instance,datetime):passelifisinstance(instance,int):instance=cls(instance,*args,**kw)else:instance=cls(*args,**kw)returninstance It requires changing the But it can't handle the call with prepended fromdatetimeimportdatetimefromtypingimportCallable,Concatenate,overload@overloaddefadd[**P](cls:Callable[P,datetime],instance:datetime|None=None,/,*args:P.args,**kwargs:P.kwargs,)->datetime: ...@overloaddefadd[**P](cls:Callable[Concatenate[int,P],datetime],instance:int,/,*args:P.args,**kwargs:P.kwargs,)->datetime: ...defadd(cls:Callable[...,datetime],instance:datetime|int|None=None,/,*args,**kw,)->datetime:ifisinstance(instance,datetime):passelifisinstance(instance,int):instance=cls(instance,*args,**kw)else:instance=cls(*args,**kw)returninstance |
BetaWas this translation helpful?Give feedback.
All reactions
-
See#2040 for newer discussion on this. |
BetaWas this translation helpful?Give feedback.