Skip to content

Commit f40b1b6

Browse files
committed
Initial stab at typing, made it through _abc and _base
1 parent cd73edd commit f40b1b6

File tree

4 files changed

+142
-65
lines changed

4 files changed

+142
-65
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ matrix:
9595
allow_failures:
9696
- env: 'TASK=docs-linkcheck INSTALL_EXTRAS=docs'
9797
- env: 'TASK=deploy-if-tag'
98-
## fast_finish causes Travis to sned a useless daily "build pending" email; not worth the noise.
98+
## fast_finish causes Travis to send a useless daily "build pending" email; not worth the noise.
9999
# fast_finish: true
100100
include:
101101
# Each job below performs a single task. Setting a per-job "TASK" env var

bidict/_abc.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@
3030

3131
from abc import abstractmethod
3232
from collections.abc import Mapping
33+
from typing import Any, Type, Mapping as MappingType, Iterator, TypeVar, Tuple, Union
3334

35+
KT = TypeVar('KT')
36+
VT = TypeVar('VT')
3437

35-
class BidirectionalMapping(Mapping): # pylint: disable=abstract-method,no-init
38+
39+
class BidirectionalMapping(MappingType[KT, VT]): # pylint: disable=abstract-method,no-init
3640
"""Abstract base class (ABC) for bidirectional mapping types.
3741
3842
Extends :class:`collections.abc.Mapping` primarily by adding the
@@ -51,7 +55,7 @@ class BidirectionalMapping(Mapping): # pylint: disable=abstract-method,no-init
5155

5256
@property
5357
@abstractmethod
54-
def inverse(self):
58+
def inverse(self) -> MappingType[VT, KT]:
5559
"""The inverse of this bidirectional mapping instance.
5660
5761
*See also* :attr:`bidict.BidictBase.inverse`, :attr:`bidict.BidictBase.inv`
@@ -65,7 +69,7 @@ def inverse(self):
6569
# clear there's no reason to call this implementation (e.g. via super() after overriding).
6670
raise NotImplementedError
6771

68-
def __inverted__(self):
72+
def __inverted__(self) -> Iterator[Tuple[VT, KT]]:
6973
"""Get an iterator over the items in :attr:`inverse`.
7074
7175
This is functionally equivalent to iterating over the items in the
@@ -82,7 +86,7 @@ def __inverted__(self):
8286
return iter(self.inverse.items())
8387

8488
@classmethod
85-
def __subclasshook__(cls, C): # noqa: N803 (argument name should be lowercase)
89+
def __subclasshook__(cls, C: 'Type[BidirectionalMapping[Any, Any]]') -> Union[bool, 'NotImplemented']: # noqa: N803 (argument name should be lowercase)
8690
"""Check if *C* is a :class:`~collections.abc.Mapping`
8791
that also provides an ``inverse`` attribute,
8892
thus conforming to the :class:`BidirectionalMapping` interface,
@@ -91,12 +95,9 @@ def __subclasshook__(cls, C): # noqa: N803 (argument name should be lowercase)
9195
"""
9296
if cls is not BidirectionalMapping: # lgtm [py/comparison-using-is]
9397
return NotImplemented
94-
if not Mapping.__subclasshook__(C):
95-
return NotImplemented
96-
mro = getattr(C, '__mro__', None)
97-
if mro is None: # Python 2 old-style class
98+
if not Mapping.__subclasshook__(C): # type: ignore
9899
return NotImplemented
99-
if not any(B.__dict__.get('inverse') for B in mro):
100+
if not any(B.__dict__.get('inverse') for B in C.__mro__):
100101
return NotImplemented
101102
return True
102103

0 commit comments

Comments
 (0)