-
Notifications
You must be signed in to change notification settings - Fork 668
feat(api)!: ListMixin.list typing overload #3122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(api)!: ListMixin.list typing overload #3122
Conversation
Thanks for your work here as well @igorp-collabora! To answer your questions:
We issue a user warning without it as it's really helped avoid frequent user questions, but I'm not sure if we should fully deprecate it/require passing it. Does it make things very difficult otherwise? /cc @JohnVillalovos
It might be a good opportunity to do this now and be more explicit. However I think we should maybe then also track removal of custom list methods (maybe we can make the list helper more generic to accommodate deviations that require custom methods now). Otherwise we'll have a lot of duplication to cover all of them. |
8cbf8a9
to
716a905
Compare
I changed the overload to be around the |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #3122 +/- ##
==========================================
+ Coverage 97.28% 97.30% +0.01%
==========================================
Files 97 97
Lines 5977 6012 +35
==========================================
+ Hits 5815 5850 +35
Misses 162 162
Flags with carried forward coverage won't be shown. Click here to find out more.
|
OK, this is really concise now! Focusing on the iterator arg also works great as that's where our custom iterator comes from as well. IMO, we could also add a breaking change trailer here to say that we no longer have union return but always a specific type, WDYT? |
Turns out that if you want to have overloaded methods extended by some other method you need to add an extra overload with original signature: import typing
@typing.overload
def foo(bar: typing.Literal[True] = True) -> int: ...
@typing.overload
def foo(bar: typing.Literal[False]) -> str: ...
#
# @typing.overload
# def foo(bar: bool) -> str | int: ...
def foo(bar: bool = True) -> str | int:
return 12345 if bar else "12345"
def test() -> None:
typing.reveal_type(foo())
typing.reveal_type(foo(True))
typing.reveal_type(foo(False))
@typing.overload
def baz(foz: typing.Literal[True] = True) -> int: ...
@typing.overload
def baz(foz: typing.Literal[False]) -> str: ...
def baz(foz: bool = True) -> int | str:
return foo(foz)
def test2() -> None:
typing.reveal_type(baz())
typing.reveal_type(baz(True))
typing.reveal_type(baz(False)) Without the 3rd overloaded signature on the
I will adjust the |
Or you define the use the union of two literals to describe the extended function args type: def baz(foz: typing.Literal[True] | typing.Literal[False] = True) -> int | str:
return foo(foz) This passed the type check. It looks like the |
I think I will add the 3rd overload instead of |
@nejch if this will be a breaking change what do you think about making the |
BREAKING CHANGE: The `http_list` method will no longer ignore the `iterator` argument if the `page` argument is not None. The `ListMixin.list()` will now return either list or iterator based on the `iterator` argument. This will make certain type narrowing checks redundant. Overload `ListMixin.list` method typing to return either `RESTObjectList` or `list` based on the `iterator` argument. By default then `iterator` is False return a list otherwise return an iterator. Provide 3 overloads: 1. `iterator: Literal[False]` - this is the default and returns a list. 2. `iterator: Literal[True]` - return an iterator. 3. `iterator: bool` - return either list or iterator. It is useful when the list function is being extended by another function that can also take either True or False for the `iterator` argument. Make `page` argument to `http_list` not override the `iterator` to make the function signatures more straight forward. This also makes it easier to unpack `**kwargs` as only `iterator` argument will control if a list or iterator is returned so the `**kwargs` can no longer have a hidden page argument.
716a905
to
33b4a70
Compare
I made the See the I added (also the |
Thanks @igorp-collabora for looking into all the options and @JohnVillalovos for taking a look as well 🙇 |
BREAKING CHANGE: The
http_list
method will no longerignore the
iterator
argument if thepage
argument is notNone. The
ListMixin.list()
will now return either list oriterator based on the
iterator
argument. This will makecertain type narrowing checks redundant.
Overload
ListMixin.list
method typing to return eitherRESTObjectList
orlist
based on theiterator
argument.By default then
iterator
is False return a list otherwisereturn an iterator.
Provide 3 overloads:
iterator: Literal[False]
- this is the default and returns a list.iterator: Literal[True]
- return an iterator.iterator: bool
- return either list or iterator. It is useful whenthe list function is being extended by another function that can
also take either True or False for the
iterator
argument.Make
page
argument tohttp_list
not override theiterator
to make the function signatures more straight forward. This
also makes it easier to unpack
**kwargs
as onlyiterator
argument will control if a list or iterator is returned so the
**kwargs
can no longer have a hidden page argument.OLDQuestions:
get_all
oriterator
raise deprecation warning? (mypy needs a fix Overloaded deprecated method use is not reported python/mypy#18474)get_all
orall
? Documentation still usesall
but apparently it was replaced in favour ofget_all
?**kwargs
are used for all options. Should at least some options become keyword arguments? This will improve code completion tools.