Add types to some internal modules (_util, _deprecate, _ki)#2719
Add types to some internal modules (_util, _deprecate, _ki)#2719A5rocks merged 24 commits intopython-trio:masterfrom
_util, _deprecate, _ki)#2719Conversation
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## master #2719 +/- ##
=======================================
Coverage 98.89% 98.90%
=======================================
Files 113 113
Lines 16600 16657 +57
Branches 3014 3021 +7
=======================================
+ Hits 16417 16474 +57
Misses 126 126
Partials 57 57
|
|
|
||
| # Equivalent to the C function raise(), which Python doesn't wrap | ||
| if os.name == "nt": | ||
| elif os.name == "nt": |
There was a problem hiding this comment.
unrelated to PR, but I've mostly seen this as sys.platform == "win32" in other files. Any reason to prefer one over the other?
There was a problem hiding this comment.
Hmm, looking at the docs, os.name is more coarse, and would just give posix I think for Linux, MacOS, etc. But actually type checkers are only required to support platform.
There was a problem hiding this comment.
git grep gives os.name 15 times across 11 files, sys.platform 57 times across 29 files. Sounds like something for a dedicated PR to replace instances of the former with the latter.
There was a problem hiding this comment.
Looking at SO, seems like os.names coarseness has it's advantages when we don't care about mac/linux distinction if we want users to be able to attempt running trio on weird:tm: systems, as sys.platform can return values outside of win32/linux/darwin.
trio/_util.py
Outdated
| """ | ||
|
|
||
| def __init__(self, msg): | ||
| def __init__(self, msg: str): |
There was a problem hiding this comment.
It's not actually required. Type checkers know that __init__ will always return None, so it's fine to leave it unannotated as long as at least one parameter is annotated to prevent the gradual typing logic from just skipping the method.
There was a problem hiding this comment.
Mypy complains if you up the strictness I'm pretty sure
There was a problem hiding this comment.
I don't have strong opinions either way, but if we want to enforce that it should be enabled - otherwise it's covered by no_untyped_defs only enforcing -> None on __init__ without typed params.
trio/__init__.py
Outdated
| @@ -1,5 +1,6 @@ | |||
| """Trio - A friendly Python library for async concurrency and I/O | |||
| """ | |||
| from __future__ import annotations # isort: skip | |||
There was a problem hiding this comment.
I don't think you should have to tell isort to skip a __future__ import
There was a problem hiding this comment.
You shouldn't no, it's because of the skip directive on the next import. But using split instead behaves more correctly.
| # We need ParamSpec to type this "properly", but that requires a runtime typing_extensions import | ||
| # to use as a class base. This is only used at runtime and isn't correct for type checkers anyway, | ||
| # so don't bother. | ||
| class generic_function(t.Generic[RetT]): |
There was a problem hiding this comment.
I think a runtime import isn't too bad, but now that I think of it I forgot we don't depend on typing_extensions iirc...
There was a problem hiding this comment.
yeah, we only depend on it for tests.
| if not t.TYPE_CHECKING: | ||
| if .* or not TYPE_CHECKING: | ||
| if .* or not _t.TYPE_CHECKING: | ||
| if .* or not t.TYPE_CHECKING: |
There was a problem hiding this comment.
nit, but could be made cleaner by properly utilizing regexes.
trio/_util.py
Outdated
| """ | ||
|
|
||
| def __init__(self, msg): | ||
| def __init__(self, msg: str): |
There was a problem hiding this comment.
I don't have strong opinions either way, but if we want to enforce that it should be enabled - otherwise it's covered by no_untyped_defs only enforcing -> None on __init__ without typed params.
| # We need ParamSpec to type this "properly", but that requires a runtime typing_extensions import | ||
| # to use as a class base. This is only used at runtime and isn't correct for type checkers anyway, | ||
| # so don't bother. | ||
| class generic_function(t.Generic[RetT]): |
There was a problem hiding this comment.
yeah, we only depend on it for tests.
CoolCat467
left a comment
There was a problem hiding this comment.
Looks good to me if everyone agrees about the pyproject.toml changes
Noticed these weren't typed, which will cause issues when typing the decorated functions. Fairly straightforward, with only two functional changes: fixing an invalid use of the deprecation logic which would include the URL twice, and making
ki_protection_enabledcoerceLOCALS_KEY_KI_PROTECTION_ENABLEDto a bool. I think that should be more correct anyway, in case someone assigns a non-bool for some reason.