Skip to content

Commit

Permalink
Enable Pyright strict mode globally.
Browse files Browse the repository at this point in the history
  • Loading branch information
jab committed Mar 13, 2022
1 parent fde6470 commit f30b6ad
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 47 deletions.
12 changes: 8 additions & 4 deletions bidict/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
from ._typing import KT, VT, MISSING, OKT, OVT, IterItems, MapOrIterItems


# Disable some pyright strict diagnostics that are not helpful in this file:
# pyright: reportPrivateUsage=false, reportUnknownArgumentType=false, reportUnknownMemberType=false, reportUnknownVariableType=false, reportUnnecessaryIsInstance=false


OldKV = t.Tuple[OKT[KT], OVT[VT]]
DedupResult = t.Optional[OldKV[KT, VT]]
Write = t.List[t.Callable[[], None]]
Expand Down Expand Up @@ -487,8 +491,8 @@ def _init_from(self, other: MapOrIterItems[KT, VT]) -> None:
self._fwdm.update(other)
# If other is a bidict, use its existing backing inverse mapping, otherwise
# other could be a generator that's now exhausted, so invert self._fwdm on the fly.
inv: MapOrIterItems[t.Any, t.Any] = other.inverse if isinstance(other, BidictBase) else inverted(self._fwdm)
self._invm.update(inv)
inv = other.inverse if isinstance(other, BidictBase) else inverted(self._fwdm)
self._invm.update(inv) # pyright: ignore

#: Used for the copy protocol.
#: *See also* the :mod:`copy` module
Expand All @@ -497,15 +501,15 @@ def _init_from(self, other: MapOrIterItems[KT, VT]) -> None:
def __or__(self: BT, other: t.Mapping[KT, VT]) -> BT:
"""Return self|other."""
if not isinstance(other, t.Mapping):
return NotImplemented # flagged by pyright
return NotImplemented
new = self.copy()
new._update(other, rbof=False)
return new

def __ror__(self: BT, other: t.Mapping[KT, VT]) -> BT:
"""Return other|self."""
if not isinstance(other, t.Mapping):
return NotImplemented # flagged by pyright
return NotImplemented
new = self.__class__(other)
new._update(self, rbof=False)
return new
Expand Down
2 changes: 1 addition & 1 deletion bidict/_frozenbidict.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class frozenbidict(BidictBase[KT, VT]):

_hash: int

# Work around lack of support for higher-kinded types in mypy.
# Work around lack of support for higher-kinded types in Python.
# Ref: https://github.com/python/typing/issues/548#issuecomment-621571821
if t.TYPE_CHECKING:
@property
Expand Down
2 changes: 1 addition & 1 deletion bidict/_frozenordered.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class FrozenOrderedBidict(OrderedBidictBase[KT, VT]):
the ordering of the items can make the ordering dependence more explicit.
"""

__hash__: t.Callable[[t.Any], int] = frozenbidict.__hash__
__hash__: t.Callable[[t.Any], int] = frozenbidict.__hash__ # pyright: ignore

if t.TYPE_CHECKING:
@property
Expand Down
4 changes: 2 additions & 2 deletions bidict/_iter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@

def iteritems_mapping_or_iterable(arg: MapOrIterItems[KT, VT]) -> IterItems[KT, VT]:
"""Yield the items in *arg* based on whether it's a mapping."""
yield from arg.items() if isinstance(arg, t.Mapping) else arg # pyright: ignore [reportGeneralTypeIssues]
yield from arg.items() if isinstance(arg, t.Mapping) else arg # pyright: ignore


def iteritems(__arg: MapOrIterItems[KT, VT], **kw: VT) -> IterItems[KT, VT]:
def iteritems(__arg: MapOrIterItems[KT, VT], **kw: VT) -> IterItems[KT, VT]: # pyright: ignore [reportMissingParameterType]
"""Yield the items from *arg* and then any from *kw* in the order given."""
yield from iteritems_mapping_or_iterable(__arg)
yield from kw.items() # type: ignore [misc]
Expand Down
5 changes: 4 additions & 1 deletion bidict/_named.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
from ._typing import KT, VT


# pyright: reportPrivateUsage=false, reportUnnecessaryIsInstance=false


class NamedBidictBase:
"""Base class that namedbidicts derive from."""

Expand Down Expand Up @@ -93,4 +96,4 @@ def _inv_cls_dict_diff(cls) -> t.Dict[str, t.Any]:
NamedInv.__doc__ = f'NamedBidictInv({basename}) {typename!r}: {valname} -> {keyname}'
caller_module = _getframe(1).f_globals.get('__name__', '__main__')
NamedBidict.__module__ = NamedInv.__module__ = caller_module
return NamedBidict
return NamedBidict # pyright: ignore [reportUnknownVariableType]
3 changes: 3 additions & 0 deletions bidict/_orderedbidict.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
from ._typing import KT, VT


# pyright: reportPrivateUsage=false


class OrderedBidict(OrderedBidictBase[KT, VT], MutableBidict[KT, VT]):
"""Mutable bidict type that maintains items in insertion order."""

Expand Down
43 changes: 5 additions & 38 deletions pyrightconfig.json
Original file line number Diff line number Diff line change
@@ -1,42 +1,9 @@
{
"exclude": [
"docs",
"tests",
"include": [
"bidict",
],
"strict": [
"bidict",
],
"reportConstantRedefinition": "error",
"reportDuplicateImport": "error",
"reportFunctionMemberAccess": "error",
"reportIncompatibleMethodOverride": "error",
"reportIncompatibleVariableOverride": "error",
"reportInvalidStringEscapeSequence": "error",
"reportInvalidStubStatement": "error",
"reportInvalidTypeVarUse": "error",
"reportMatchNotExhaustive": "error",
"reportMissingTypeArgument": "error",
"reportMissingTypeStubs": "error",
"reportOverlappingOverload": "error",
"reportPropertyTypeMismatch": "error",
"reportSelfClsParameterName": "error",
"reportUnknownLambdaType": "error",
"reportUnknownParameterType": "error",
"reportUnnecessaryTypeIgnoreComment": "warning",
"reportUnsupportedDunderAll": "error",
"reportUntypedBaseClass": "error",
"reportUntypedClassDecorator": "error",
"reportUntypedFunctionDecorator": "error",
"reportUntypedNamedTuple": "error",
"reportUnusedClass": "error",
"reportUnusedFunction": "error",
"reportUnusedImport": "error",
"reportUnusedVariable": "error",
"strictDictionaryInference": true,
"strictListInference": true,
"strictParameterNoneValue": true,

// Leave these off for now:
"reportPrivateUsage": "none",
"reportUnknownArgumentType": "none",
"reportUnknownMemberType": "none",
"reportUnknownVariableType": "none",
"reportUnusedCallResult": "none",
}

0 comments on commit f30b6ad

Please sign in to comment.