Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

problems implementing overloads on some flexible class methods#2040

Unanswered
cjw296 asked this question inQ&A
Discussion options

Hi All,

I eventually managed to release apy.typed version of testfixtures, but to do so I had to give up on a bunch of overloads which I though helped document intended methods of use:

simplistix/testfixtures@70283c2

I've tried to get my head around this a few times over the last few months:

I even ended up logging amypy bug:

...and as well as several attempts myself, LLMs have also been struggling, even once the underlying typing was sorted out:

At a high level, I'm essentially looking to revertsimplistix/testfixtures@70283c2 but with functioning type annotations, essentially providing these three calling patterns for the factory:

@overloaddefmock_datetime(tzinfo:TZInfo|None=None,delta:float|None=None,delta_type:str='seconds',date_type:type[date]=date,strict:bool=False)->type[MockDateTime]:    ...@overloaddefmock_datetime(year:int,month:int,day:int,hour:int= ...,minute:int= ...,second:int= ...,microsecond:int= ...,tzinfo:TZInfo|None=None,delta:float|None=None,delta_type:str='seconds',date_type:type[date]=date,strict:bool=False)->type[MockDateTime]:    ...@overloaddefmock_datetime(default:datetime,tzinfo:TZInfo|None=None,delta:float|None=None,delta_type:str='seconds',date_type:type[date]=date,strict:bool=False)->type[MockDateTime]:    ...@overloaddefmock_datetime(default:None,# explicit None positionaltzinfo:TZInfo|None=None,delta:float|None=None,delta_type:str='seconds',date_type:type[date]=date,strict:bool=False)->type[MockDateTime]:    ...defmock_datetime(*args,tzinfo:TZInfo|None=None,

...and then these overload patterns for the methods for manipulating the queue of upcoming mock datetimes:

classMockDateTime(MockedCurrent,datetime):@overload@classmethoddefadd(cls,year:int,month:int,day:int,hour:int= ...,minute:int= ...,second:int= ...,microsecond:int= ...,tzinfo:TZInfo= ...,    )->None:        ...@overload@classmethoddefadd(cls,instance:datetime,    )->None:        ...@classmethoddefadd(cls,*args,**kw):"""@@ -170,29 +147,6 @@ def add(cls, *args, **kw):        """returnsuper().add(*args,**kw)@overload@classmethoddefset(cls,year:int,month:int,day:int,hour:int= ...,minute:int= ...,second:int= ...,microsecond:int= ...,tzinfo:TZInfo= ...,    )->None:        ...@overload@classmethoddefset(cls,instance:datetime,    )->None:        ...@classmethoddefset(cls,*args,**kw):"""@@ -205,28 +159,6 @@ def set(cls, *args, **kw):        """returnsuper().set(*args,**kw)@overload@classmethoddeftick(cls,days:float= ...,seconds:float= ...,microseconds:float= ...,milliseconds:float= ...,minutes:float= ...,hours:float= ...,weeks:float= ...,    )->None:        ...@overload@classmethoddeftick(cls,delta:timedelta,# can become positional-only when Python 3.8 minimum    )->None:        ...@classmethoddeftick(cls,*args,**kw)->None:        """

...and similar acrossmock_datetime,mock_time andmock_date and the helper types they construct.

Is Python typing just "not there yet" or am I missing something? Happy to be shown the way, ecstatic if someone feels inspired to throw up a PR 😅

You must be logged in to vote

Replies: 1 comment 2 replies

Comment options

Can you describe the problem you're seeing in more detail? You mentioned in the other thread that the ellipses make mypy unhappy. When I paste your code into a new document, add the missing import statements, and run mypy on it, I don't see any errors.

Normally, you'd need to supplyreal default argument values rather than using an ellipsis, but type checkers generally allow you to use an ellipsis as a stand-in for the actual value when you're writing overloads or stubs. It's still a good idea to provide the actual default argument values because it allows language servers to show users these values when they call the function. For example, here's what I see when I start typing a call toMockDateTime.set when using the pyright language server.

image

It would be better if you supplied the actual default values. I presume they are zero by default? If so, replace the... with0.

You must be logged in to vote
2 replies
@cjw296
Comment options

The problems were always intractable mypy complaints of a variety of types; to reproduce: pick one of the claude or codex branches above, check it out and runuv mypy src/ tests/.

I'm fine with there being no ellipsis, I wrote that years ago before I understood typing and when tools like mypy were way less mature.

To be clear: the ellipsis are not the focus of the problem for me, the problem is that no human or agent that has tried can get overloads that both match the intended use semanticsand make mypy happy.

@erictraut
Comment options

You're more likely to receive answers on this forum if you can narrow the question. "I can't get this to work; can you check out my code base, grok the intent behind the code, and recommend a way to write type annotations for my library?" is pretty broad. Perhaps you could post one or two examples of an overload that youthink should work but does not?

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@cjw296@erictraut

[8]ページ先頭

©2009-2025 Movatter.jp