-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial stab at typing, made it through _abc and _base
- Loading branch information
Showing
4 changed files
with
142 additions
and
65 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
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 |
---|---|---|
|
@@ -30,9 +30,13 @@ | |
|
||
from abc import abstractmethod | ||
from collections.abc import Mapping | ||
from typing import Any, Type, Mapping as MappingType, Iterator, TypeVar, Tuple, Union | ||
|
||
KT = TypeVar('KT') | ||
VT = TypeVar('VT') | ||
|
||
class BidirectionalMapping(Mapping): # pylint: disable=abstract-method,no-init | ||
|
||
class BidirectionalMapping(MappingType[KT, VT]): # pylint: disable=abstract-method,no-init | ||
"""Abstract base class (ABC) for bidirectional mapping types. | ||
Extends :class:`collections.abc.Mapping` primarily by adding the | ||
|
@@ -51,7 +55,7 @@ class BidirectionalMapping(Mapping): # pylint: disable=abstract-method,no-init | |
|
||
@property | ||
@abstractmethod | ||
def inverse(self): | ||
def inverse(self) -> MappingType[VT, KT]: | ||
"""The inverse of this bidirectional mapping instance. | ||
*See also* :attr:`bidict.BidictBase.inverse`, :attr:`bidict.BidictBase.inv` | ||
|
@@ -65,7 +69,7 @@ def inverse(self): | |
# clear there's no reason to call this implementation (e.g. via super() after overriding). | ||
raise NotImplementedError | ||
|
||
def __inverted__(self): | ||
def __inverted__(self) -> Iterator[Tuple[VT, KT]]: | ||
"""Get an iterator over the items in :attr:`inverse`. | ||
This is functionally equivalent to iterating over the items in the | ||
|
@@ -82,7 +86,7 @@ def __inverted__(self): | |
return iter(self.inverse.items()) | ||
|
||
@classmethod | ||
def __subclasshook__(cls, C): # noqa: N803 (argument name should be lowercase) | ||
def __subclasshook__(cls, C: 'Type[BidirectionalMapping[Any, Any]]') -> Union[bool, 'NotImplemented']: # noqa: N803 (argument name should be lowercase) | ||
This comment has been minimized.
Sorry, something went wrong.
jab
Owner
|
||
"""Check if *C* is a :class:`~collections.abc.Mapping` | ||
that also provides an ``inverse`` attribute, | ||
thus conforming to the :class:`BidirectionalMapping` interface, | ||
|
@@ -91,12 +95,9 @@ def __subclasshook__(cls, C): # noqa: N803 (argument name should be lowercase) | |
""" | ||
if cls is not BidirectionalMapping: # lgtm [py/comparison-using-is] | ||
return NotImplemented | ||
if not Mapping.__subclasshook__(C): | ||
return NotImplemented | ||
mro = getattr(C, '__mro__', None) | ||
if mro is None: # Python 2 old-style class | ||
This comment has been minimized.
Sorry, something went wrong.
jab
Owner
|
||
if not Mapping.__subclasshook__(C): # type: ignore | ||
return NotImplemented | ||
if not any(B.__dict__.get('inverse') for B in mro): | ||
if not any(B.__dict__.get('inverse') for B in C.__mro__): | ||
return NotImplemented | ||
return True | ||
|
||
|
Oops, something went wrong.
(Thanks for the typo fix! :)