Skip to content

Commit

Permalink
Make None compatible with SupportsBool protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
blablatdinov committed Aug 16, 2023
1 parent 76c16a4 commit bc684ea
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,9 @@ def visit_none_type(self, left: NoneType) -> bool:
# None is compatible with Hashable (and other similar protocols). This is
# slightly sloppy since we don't check the signature of "__hash__".
# None is also compatible with `SupportsStr` protocol.
return not members or all(member in ("__hash__", "__str__") for member in members)
return not members or all(
member in ("__hash__", "__str__", "__bool__") for member in members
)
return False
else:
return True
Expand Down
6 changes: 6 additions & 0 deletions test-data/unit/check-protocols.test
Original file line number Diff line number Diff line change
Expand Up @@ -2890,6 +2890,12 @@ class SupportsStr(Protocol):
def ss(s: SupportsStr) -> None: pass
ss(None)

class SupportsBool(Protocol):
def __bool__(self) -> bool: ...

def sb(s: SupportsBool) -> None: pass
sb(None)

class HashableStr(Protocol):
def __str__(self) -> str: ...
def __hash__(self) -> int: ...
Expand Down

0 comments on commit bc684ea

Please sign in to comment.