Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed typing to work with the --disallow-any-generics flag on vistautils #73

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions immutablecollections/_immutabledict.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from abc import ABCMeta
from typing import (
Any,
Callable,
Dict,
Generic,
Expand All @@ -19,21 +20,23 @@

KT = TypeVar("KT")
VT = TypeVar("VT")
IT = Tuple[KT, VT]

# cannot share type variables between outer and inner classes
KT2 = TypeVar("KT2")
VT2 = TypeVar("VT2")
IT2 = Tuple[KT2, VT2]

SelfType = TypeVar("SelfType") # pylint:disable=invalid-name

AllowableSourceType = Union[Iterable[IT], Mapping[KT, VT], "ImmutableDict[KT, VT]"]
AllowableSourceType = Union[
Iterable[Tuple[KT, VT]], Mapping[KT, VT], "ImmutableDict[KT, VT]"
]
InstantiationTypes = (Mapping, Iterable) # pylint:disable=invalid-name


def immutabledict(
iterable: Optional[AllowableSourceType] = None, *, forbid_duplicate_keys: bool = False
iterable: Optional[AllowableSourceType[Any, Any]] = None,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if you use KT, VT instead of Any, Any?

Copy link
Collaborator Author

@jamart28 jamart28 Feb 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It throws errors on vistautils saying that typing doesn't match. For some reason it tries to match to KT and VT exactly instead of using them as typevars. This can be seen as the original fix committed used typevars directly which mypy was okay with despite it being a syntax error on the typing. Furthermore, the errors thrown by mypy when using KT, VT were not errors from the flags so everyone who uses immutablecollections would be affected even if we just # type: ignoreed on vistautils. @gabbard

*,
forbid_duplicate_keys: bool = False,
) -> "ImmutableDict[KT, VT]":
"""
Create an immutable dictionary with the given mappings.
Expand Down Expand Up @@ -221,7 +224,7 @@ def put(self: SelfType, key: KT2, val: VT2) -> SelfType:
return self

def put_all(
self: SelfType, data: Union[Mapping[KT2, VT2], Iterable[IT2]]
self: SelfType, data: Union[Mapping[KT2, VT2], Iterable[Tuple[KT2, VT2]]]
) -> SelfType:
if isinstance(data, Mapping):
for (k, v) in data.items():
Expand Down