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

How do I type an overloaded function's keyword arguments that may not be specified? #1919

Unanswered
cjw296 asked this question in Q&A
Discussion options

Hand-waving, I have this:

class MockDateTime:
 @overload
 @classmethod
 def set(
 cls,
 year: int,
 month: int,
 day: int,
 hour: int = ...,
 minute: int = ...,
 second: int = ...,
 microsecond: int = ...,
 tzinfo: TZInfo = ...,
 ) -> None:
 ...
 @overload
 @classmethod
 def set(
 cls,
 instance: datetime,
 /
 ) -> None:
 ...
 @classmethod
 def set(cls, *args: int | datetime, **kw: int) -> None:
 """
 This will set the :class:`datetime.datetime` created from the
 supplied parameters as the next datetime to be returned by
 :meth:`~MockDateTime.now` or :meth:`~MockDateTime.utcnow`, clearing out
 any datetimes in the queue. An instance
 of :class:`~datetime.datetime` may also be passed as a single
 positional argument.
 """

So, those ellipsis make mypy unhappy, but how do I express "these may not be present, but if they are, they must be ints, or TZInfo in the case of tzinfo"?

Note, this is explicitly not what I want, since it's not valid to pass None for these:

class MockDateTime:
 @overload
 @classmethod
 def set(
 cls,
 year: int,
 month: int,
 day: int,
 hour: int = None,
 minute: int = None,
 second: int = None,
 microsecond: int = None,
 tzinfo: TZInfo = None,
 ) -> None:
 ...
You must be logged in to vote

Replies: 1 comment

Comment options

See #2040 for newer discussion on this.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /