type hints: (trivial) add missing "Optional" qualifiers - #10903
Open
SomberNight wants to merge 4 commits into
Open
type hints: (trivial) add missing "Optional" qualifiers#10903SomberNight wants to merge 4 commits into
SomberNight wants to merge 4 commits into
Conversation
In many cases with our type hinting, we do not explicitly specify that variables are allowed to be None when they have a default value of None.
This PR now adds the missing explicit "Optional" markers.
-----
Consider:
```
class Foo(NamedTuple):
field: str = None
```
This type-hinting is technically incorrect, we should type hint it as either one of these options:
- `field: Optional[str] = None`
- `field: str | None = None`
PyCharm has had soft-warnings about these lines for quite a while,
however when doing type-inference, it used to internally "fix" the type by allowing None.
Recently however it seems it is not doing the internal inference-fixing anymore, but assume that `path` has type `str` (as we specified) and then make "incorrect" inferences:
```
class Foo(NamedTuple):
field: str = None
def func(foo: Foo):
assert foo.field is None
print("function body") # <<< PyCharm now says this code is unreachable
```
I have whole function bodies grayed out and marked as dead code due to this.
Example: https://github.com/spesmilo/electrum/blob/c82a0d12ef1e03a96d6180839f215c05efe9efe3/electrum/lnpeer.py#L2320, where htlc_set.parent_set_key is type-hinted as str, so the assert "must" always fail
harmless in current usage.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In many cases with our type hinting, we do not explicitly specify that variables are allowed to be None when they have a default value of None.
This PR now adds the missing explicit "Optional" markers.
Consider:
This type-hinting is technically incorrect, we should type hint it as either one of these options:
field: Optional[str] = Nonefield: str | None = NoneThomas said he prefers the second, so
str | None.However I am not going to touch lines with existing
Optionals, I am only fixing lines that are type-hinted incorrectly.PyCharm has had soft-warnings about these lines for quite a while,
however when doing type-inference, it used to internally "fix" the type by allowing None.
Recently however it seems it is not doing the internal inference-fixing anymore, but assumes that
pathhas typestr(as we specified) and then make "incorrect" inferences:I have whole function bodies grayed out and marked as dead code due to this.
Example:
electrum/electrum/lnpeer.py
Line 2320 in c82a0d1