Skip to content

Commit

Permalink
Finish first pass at adding type hints.
Browse files Browse the repository at this point in the history
Fixes #93.
  • Loading branch information
jab committed Aug 2, 2020
1 parent 8a8e58b commit 2f7e5ee
Show file tree
Hide file tree
Showing 27 changed files with 333 additions and 270 deletions.
12 changes: 4 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ repos:
# - id: fix-encoding-pragma
- id: check-yaml

#- repo: https://github.com/pre-commit/mirrors-mypy
# rev: '0.782'
# hooks:
# - id: mypy
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.782
hooks:
- id: mypy

- repo: https://github.com/pycqa/pydocstyle
rev: 5.0.2
Expand Down Expand Up @@ -53,10 +53,6 @@ repos:
- hypothesis < 6
- pytest < 7
- Sphinx < 4
args:
# http://pylint.pycqa.org/en/latest/user_guide/run.html#parallel-execution
# "If the provided number is 0, then the total number of CPUs will be used."
- --jobs=0

- repo: https://github.com/jumanjihouse/pre-commit-hooks
rev: 2.1.4
Expand Down
41 changes: 23 additions & 18 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
# https://docs.pylint.org/en/latest/technical_reference/features.html

[MASTER]
ignore=_version.py conf.py
jobs=0

[MESSAGES CONTROL]
disable =
disable=
abstract-method,
arguments-differ,
attribute-defined-outside-init,
bad-continuation,
broad-except,
invalid-name,
isinstance-second-argument-not-valid-type, # https://github.com/PyCQA/pylint/issues/3507
line-too-long,
no-init,
no-member,
too-few-public-methods,
not-callable,
protected-access,
isinstance-second-argument-not-valid-type, # https://github.com/PyCQA/pylint/issues/3507

# bidict/_version.py is generated by setuptools_scm
ignore = _version.py

# Maximum number of parents for a class.
# The default of 7 results in "too-many-ancestors" for all bidict classes.
max-parents = 13

# Maximum number of arguments for a function.
# The default of 5 only leaves room for 4 args besides self for methods.
max-args=6


[FORMAT]
max-line-length=125
redefined-builtin,
signature-differs,
super-init-not-called,
too-few-public-methods,
too-many-ancestors,
too-many-branches,
too-many-locals,
wrong-import-order,
wrong-import-position,
12 changes: 11 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,17 @@ to be notified when new versions of ``bidict`` are released.

- Drop support for Python 3.5.

- Remove the ``bidict.compat`` module.
- Remove the no-longer-needed ``bidict.compat`` module.

- :meth:`bidict.OrderedBidictBase.__iter__` no longer accepts a ``reverse`` keyword arg
so as not to differ from the signature of :meth:`collections.abc.Mapping.__iter__`.

- Set :func:`~bidict.namedbidict` classes' :attr:`~definition.__module__`
to the module of the caller via :func:`sys._getframe`,
rather than leaving it set to ``'bidict._named'``.

Also set :attr:`~definition.__qualname__` to the provided ``typename`` argument,
so that it gets the same value as :attr:`~definition.__name__`.


0.20.0 (2020-07-23)
Expand Down
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Status
safety, simplicity, flexibility, and ergonomics
- is fast, lightweight, and has no runtime dependencies other than Python's standard library
- integrates natively with Python’s collections interfaces
- provides type hints for all public APIs
- is implemented in concise, well-factored, pure (PyPy-compatible) Python code
optimized both for reading and learning from [#fn-learning]_
as well as for running efficiently
Expand Down
1 change: 0 additions & 1 deletion bidict/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@

# The rest of this file only collects functionality implemented in the rest of the
# source for the purposes of exporting it under the `bidict` module namespace.
# pylint: disable=wrong-import-position
# flake8: noqa: F401 (imported but unused)
from ._abc import BidirectionalMapping, MutableBidirectionalMapping
from ._base import BidictBase
Expand Down
29 changes: 20 additions & 9 deletions bidict/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,28 @@

"""Provide the :class:`BidirectionalMapping` abstract base class."""

import typing as _t
from abc import abstractmethod
from typing import AbstractSet, Iterator, Mapping, MutableMapping, Tuple, TypeVar


KT = TypeVar('KT')
VT = TypeVar('VT')
class _SntlMeta(type):
def __repr__(cls) -> str: # pragma: no cover
return f'<{cls.__name__}>'


# pylint: disable=abstract-method,no-init
class _NONE(metaclass=_SntlMeta):
"""Private sentinel type, used to represent e.g. missing values."""

class BidirectionalMapping(Mapping[KT, VT]):

KT = _t.TypeVar('KT') # key type
VT = _t.TypeVar('VT') # value type
OKT = _t.Union[KT, _NONE] # optional key type
OVT = _t.Union[VT, _NONE] # optional value type
IterItems = _t.Iterable[_t.Tuple[KT, VT]]
MapOrIterItems = _t.Union[_t.Mapping[KT, VT], IterItems[KT, VT]]


class BidirectionalMapping(_t.Mapping[KT, VT]):
"""Abstract base class (ABC) for bidirectional mapping types.
Extends :class:`collections.abc.Mapping` primarily by adding the
Expand Down Expand Up @@ -66,7 +77,7 @@ def inverse(self) -> 'BidirectionalMapping[VT, KT]':
# clear there's no reason to call this implementation (e.g. via super() after overriding).
raise NotImplementedError

def __inverted__(self) -> Iterator[Tuple[VT, KT]]:
def __inverted__(self) -> _t.Iterator[_t.Tuple[VT, KT]]:
"""Get an iterator over the items in :attr:`inverse`.
This is functionally equivalent to iterating over the items in the
Expand All @@ -82,7 +93,7 @@ def __inverted__(self) -> Iterator[Tuple[VT, KT]]:
"""
return iter(self.inverse.items())

def values(self) -> AbstractSet[VT]: # type: ignore[override]
def values(self) -> _t.KeysView[VT]: # type: ignore
"""A set-like object providing a view on the contained values.
Override the implementation inherited from
Expand All @@ -94,10 +105,10 @@ def values(self) -> AbstractSet[VT]: # type: ignore[override]
which has the advantages of constant-time containment checks
and supporting set operations.
"""
return self.inverse.keys()
return self.inverse.keys() # type: ignore


class MutableBidirectionalMapping(BidirectionalMapping[KT, VT], MutableMapping[KT, VT]):
class MutableBidirectionalMapping(BidirectionalMapping[KT, VT], _t.MutableMapping[KT, VT]):
"""Abstract base class (ABC) for mutable bidirectional mapping types."""

__slots__ = ()
Expand Down
Loading

0 comments on commit 2f7e5ee

Please sign in to comment.