Skip to content
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

Fixed inability to pass total_tokens as a keyword argument on trio #666

Merged
merged 3 commits into from
Jan 13, 2024

Conversation

agronholm
Copy link
Owner

Fixes #515.

Comment on lines 641 to +651
def __init__(
self, *args: Any, original: trio.CapacityLimiter | None = None
self,
total_tokens: float | None = None,
*,
original: trio.CapacityLimiter | None = None,
) -> None:
self.__original = original or trio.CapacityLimiter(*args)
if original is not None:
self.__original = original
else:
assert total_tokens is not None
self.__original = trio.CapacityLimiter(total_tokens)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you want, you could tell the type checker about this:

+    @overload
+    def __init__(self, total_tokens: float) -> None:
+        ...
+
+    @overload
+    def __init__(self, *, original: trio.CapacityLimiter) -> None:
+        ...
+
     def __init__(

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure there's much point, as this is an internal thing. And wouldn't I also have to do the same with __new__()?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure there's much point, as this is an internal thing.

agreed.

And wouldn't I also have to do the same with __new__()?

no, because in a call CapacityLimiter(...) the type checker must ensure that the arguments are valid to pass to both __new__ and __init__. if at least one of the two has the overloads (i.e. has the extra strictness), the type checker will reject invalid calls like CapacityLimiter(1, original=trio.CapacityLimiter(1)). they do not both need to specify the full strictness of the overload.

tested: with both Mypy and pyright, the call CapacityLimiter(1, original=trio.CapacityLimiter(1)) is rejected when __init__ is overloaded, no matter whether the overload is copied to __new__ too or not.

@agronholm agronholm merged commit 7878248 into master Jan 13, 2024
16 checks passed
@agronholm agronholm deleted the fix-515 branch January 13, 2024 11:17
@agronholm
Copy link
Owner Author

Thanks for the review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

On trio backend, CapacityLimiter does not take keyword arguments
2 participants