This repository has been archived by the owner on Jul 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix typing issues of StringSetFlag (#107)
* Selectively ignore override errors as this is a custom class * Use a separate interface declaration file (.pyi) by refactoring out StringSetFlag class from the utils module to overcome limitation of mypy semantic analysis on "__ror__ = __or__" in subclasses of Enum/Flag classes. Backported-From: main (22.03) Backported-To; 21.03
- Loading branch information
Showing
5 changed files
with
84 additions
and
56 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix typing issues of `StringSetFlag` by refactoring it using a separate interface definition file |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from __future__ import annotations | ||
|
||
import enum | ||
|
||
__all__ = ( | ||
'StringSetFlag', | ||
) | ||
|
||
|
||
class StringSetFlag(enum.Flag): | ||
|
||
def __eq__(self, other): | ||
return self.value == other | ||
|
||
def __hash__(self): | ||
return hash(self.value) | ||
|
||
def __or__(self, other): | ||
if isinstance(other, type(self)): | ||
other = other.value | ||
if not isinstance(other, (set, frozenset)): | ||
other = set((other,)) | ||
return set((self.value,)) | other | ||
|
||
__ror__ = __or__ | ||
|
||
def __and__(self, other): | ||
if isinstance(other, (set, frozenset)): | ||
return self.value in other | ||
if isinstance(other, str): | ||
return self.value == other | ||
raise TypeError | ||
|
||
__rand__ = __and__ | ||
|
||
def __xor__(self, other): | ||
if isinstance(other, (set, frozenset)): | ||
return set((self.value,)) ^ other | ||
if isinstance(other, str): | ||
if other == self.value: | ||
return set() | ||
else: | ||
return other | ||
raise TypeError | ||
|
||
def __rxor__(self, other): | ||
if isinstance(other, (set, frozenset)): | ||
return other ^ set((self.value,)) | ||
if isinstance(other, str): | ||
if other == self.value: | ||
return set() | ||
else: | ||
return other | ||
raise TypeError | ||
|
||
def __str__(self): | ||
return self.value |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import enum | ||
|
||
|
||
class StringSetFlag(enum.Flag): | ||
def __eq__(self, other: object) -> bool: ... | ||
def __hash__(self) -> int: ... | ||
def __or__( # type: ignore[override] | ||
self, | ||
other: StringSetFlag | str | set[str] | frozenset[str], | ||
) -> set[str]: ... | ||
def __and__( # type: ignore[override] | ||
self, | ||
other: StringSetFlag | str | set[str] | frozenset[str], | ||
) -> bool: ... | ||
def __xor__( # type: ignore[override] | ||
self, | ||
other: StringSetFlag | str | set[str] | frozenset[str], | ||
) -> set[str]: ... | ||
def __ror__(self, other: StringSetFlag | str | set[str] | frozenset[str]) -> set[str]: ... | ||
def __rand__(self, other: StringSetFlag | str | set[str] | frozenset[str]) -> bool: ... | ||
def __rxor__(self, other: StringSetFlag | str | set[str] | frozenset[str]) -> set[str]: ... | ||
def __str__(self) -> str: ... |
This file contains 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
This file contains 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