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

Merge type stubs #1873

Closed
wants to merge 52 commits into from
Closed

Merge type stubs #1873

wants to merge 52 commits into from

Conversation

altendky
Copy link
Member

@altendky altendky commented Jan 22, 2021

Draft for:

@codecov
Copy link

codecov bot commented Jan 22, 2021

Codecov Report

Merging #1873 (9ecbc1d) into master (ff86c60) will decrease coverage by 0.37%.
The diff coverage is 96.92%.

@@            Coverage Diff             @@
##           master    #1873      +/-   ##
==========================================
- Coverage   99.59%   99.21%   -0.38%     
==========================================
  Files         114      115       +1     
  Lines       14574    14914     +340     
  Branches     1110     1115       +5     
==========================================
+ Hits        14515    14797     +282     
- Misses         42       96      +54     
- Partials       17       21       +4     
Impacted Files Coverage Δ
trio/_subprocess_platform/waitid.py 66.66% <50.00%> (-2.57%) ⬇️
trio/_file_io.py 73.94% <63.72%> (-26.06%) ⬇️
trio/_tools/gen_exports.py 94.50% <70.58%> (-5.50%) ⬇️
trio/_typing.py 85.71% <85.71%> (ø)
trio/_highlevel_open_unix_stream.py 89.65% <87.50%> (-1.65%) ⬇️
trio/_highlevel_socket.py 98.30% <89.47%> (-1.70%) ⬇️
trio/_core/_io_kqueue.py 85.82% <90.47%> (+0.34%) ⬆️
trio/_core/_local.py 97.05% <93.33%> (-2.95%) ⬇️
trio/tests/test_subprocess.py 98.67% <93.44%> (-1.33%) ⬇️
trio/_core/_multierror.py 98.93% <94.28%> (-1.07%) ⬇️
... and 89 more

@@ -219,7 +236,7 @@ def acquire_nowait(self):
self.acquire_on_behalf_of_nowait(trio.lowlevel.current_task())

@enable_ki_protection
def acquire_on_behalf_of_nowait(self, borrower):
def acquire_on_behalf_of_nowait(self, borrower: Union[object, Task]) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

Union[object, Task] simplifies to object

Copy link
Member

@graingert graingert Feb 28, 2021

Choose a reason for hiding this comment

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

@altendky you probably want CapacityLimiter to be generic so you can do:

Suggested change
def acquire_on_behalf_of_nowait(self, borrower: Union[object, Task]) -> None:
def acquire_on_behalf_of_nowait(self, borrower: T) -> None:

so you can do:

class MyResource:
    def __init__(self):
        self._limit: trio.CapacityLimiter[MyResource] = trio.CapacityLimiter(40)

    def acquire(self):
        self._limit.acquire_on_behalf_of_nowait(self)
    def release(self):
        self._limit.release_on_behalf_of(self)

Copy link
Member

Choose a reason for hiding this comment

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

the methods that use a task would have to refine the self type:

    @enable_ki_protection
    def acquire_nowait(self: _CapacityLimiter[abc.Task[object]]) -> None:
        """Borrow a token from the sack, without blocking.
        Raises:
          WouldBlock: if no tokens are available.
          RuntimeError: if the current task already holds one of this sack's
              tokens.
        """
        self.acquire_on_behalf_of_nowait(trio.lowlevel.current_task())

Copy link
Member Author

Choose a reason for hiding this comment

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

Hints like Union[object, Task] were pretty intentional, but open to change. In this case it mirrored the documentation describing a Task as a common particular type of object used here. In other cases, code actually treated the specific type differently. Given the suggestion of a more complicated change that I'm not going to get to right now, I'll leave it as is for now.

With the scale of this PR I expect tasks like this to get lost. I'll make an explicit list of them in the OP with links. Maybe I'll get lucky and find it is pointlessly redundant.

If there's any interest in bulk contributing to the PR... I'd welcome it. :] There's lots more hinting to add, even as just a 'first pass' to satisfy not-so-loose Mypy settings.

return self

async def __aexit__(self, *args):
async def __aexit__(self, *args: object) -> None:
Copy link

Choose a reason for hiding this comment

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

shouldn't that be

async def __aexit__(self, exc_type: Optional[Type[BaseException]],
        exc_value: Optional[BaseException],
        exc_trackback: Optional[TracebackType]) -> Optional[bool]:

As that is the type the contextmanager protocol uses
(Maybe with added = Nones, not sure about that)

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think this helps anything, but it does seem more proper. Though I think -> None is an accurate and good expression of the behavior. Thanks.

Added to the list in the OP.

Copy link

Choose a reason for hiding this comment

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

using -> None would disallow subclasses from returning True if they have some reason for that
(returning None and False have the same effect)

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. Thanks for following up.

@Zac-HD
Copy link
Member

Zac-HD commented Jul 21, 2023

Closing this as stale; it looks like we're taking a more incremental approach - so far in #2477, #2671, #2682, and generally with https://github.com/python-trio/trio/pulls?q=is%3Apr+label%3Atyping. I really appreciate all the work that went into this though!

@Zac-HD Zac-HD closed this Jul 21, 2023
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.

4 participants