python-trio / trio

Trio – a friendly Python library for async concurrency and I/O
https://trio.readthedocs.io
Other
5.98k stars 325 forks source link

Path decorators: allow empty docstrings to support embedded Python #2979

Closed ameily closed 2 months ago

ameily commented 3 months ago

I am developing an application that runs on the Windows embedded Python package. It appears that the embedded Python standard library does not include docstrings which means an unhandled exception is raised when importing trio because of a couple assert X.__doc__ is not None statements. This PR makes it so __doc__ is allowed to be None.

To test, I am using Python Windows Embedded 64-bit 3.11.8 package. I wrote a small script to show that __doc__ is None on every Path function:

import inspect
from pathlib import Path

for name in dir(Path):
    if name.startswith("_"):
        continue

    attr = getattr(Path, name)
    if inspect.isfunction(attr) or inspect.ismethod(attr):
        if attr.__doc__ is None:
            print(f"Path.{name}.__doc__ is None")

When I run this through embedded Python I see every function in Path has no docstring.

codecov[bot] commented 3 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 99.62%. Comparing base (e87dc46) to head (b519645).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #2979 +/- ## ========================================== - Coverage 99.63% 99.62% -0.01% ========================================== Files 117 117 Lines 17598 17602 +4 Branches 3171 3174 +3 ========================================== + Hits 17533 17536 +3 - Misses 46 47 +1 Partials 19 19 ``` | [Files](https://app.codecov.io/gh/python-trio/trio/pull/2979?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-trio) | Coverage Δ | | |---|---|---| | [src/trio/\_path.py](https://app.codecov.io/gh/python-trio/trio/pull/2979?src=pr&el=tree&filepath=src%2Ftrio%2F_path.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-trio#diff-c3JjL3RyaW8vX3BhdGgucHk=) | `100.00% <100.00%> (ø)` | | | [src/trio/\_tests/test\_path.py](https://app.codecov.io/gh/python-trio/trio/pull/2979?src=pr&el=tree&filepath=src%2Ftrio%2F_tests%2Ftest_path.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-trio#diff-c3JjL3RyaW8vX3Rlc3RzL3Rlc3RfcGF0aC5weQ==) | `100.00% <100.00%> (ø)` | | ... and [1 file with indirect coverage changes](https://app.codecov.io/gh/python-trio/trio/pull/2979/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-trio)
A5rocks commented 3 months ago

IMO we shouldn't set the docstring if there isn't one, rather than acting as though the docstring is blank.

TeamSpen210 commented 3 months ago

That would make sense yes. This might be tricky to test for though - Trio can't work in -OO mode, because pycparser (imported from cffi) needs docstrings to work. But it would be fine if only the standard library is stripped.

jakkdl commented 2 months ago

Could add tests which monkeypatch Path and then runs the decorator on them, which would appease codecov.

A5rocks commented 2 months ago

I added a test case cause it can be annoying to make them. cc @jakkdl does it look good?

(the only iffy bit is passing a sync function to a function that assumes it is async -- but it's not like we run it and we can update that if we ever require passed in functions to satisfy inspect.iscoroutinefunction)

A5rocks commented 2 months ago

@ameily I'm super flakey on these (so you can ignore this if you want) but you don't have a changelog attached! Might be nice to have that.

jakkdl commented 2 months ago

I added a test case cause it can be annoying to make them. cc @jakkdl does it look good?

(the only iffy bit is passing a sync function to a function that assumes it is async -- but it's not like we run it and we can update that if we ever require passed in functions to satisfy inspect.iscoroutinefunction)

looks great! (other than needing a # pragma: no cover :P)

trio-bot[bot] commented 2 months ago

Hey @ameily, it looks like that was the first time we merged one of your PRs! Thanks so much! :tada: :birthday:

If you want to keep contributing, we'd love to have you. So, I just sent you an invitation to join the python-trio organization on Github! If you accept, then here's what will happen:

If you want to read more, here's the relevant section in our contributing guide.

Alternatively, you're free to decline or ignore the invitation. You'll still be able to contribute as much or as little as you like, and I won't hassle you about joining again. But if you ever change your mind, just let us know and we'll send another invitation. We'd love to have you, but more importantly we want you to do whatever's best for you.

If you have any questions, well... I am just a humble Python script, so I probably can't help. But please do post a comment here, or in our chat, or on our forum, whatever's easiest, and someone will help you out!