isi-vista / immutablecollections

A library for immutable collections, in the spirit of Guava's Immutable Collections.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Implicit Any in `immutabledict` throws error on vistautils#86

jamart28 opened this issue · comments

Implicit Any in immutabledict throws error on vistautils#86

Tracing problem through imports to find issue

It seems like the issue was that the aliases were expecting something passed through as they were being considered generics. As such one fix I'm currently trying is to not use the aliases. However this presents the following errors in vistautils (after installing the update version locally and using it):
image Any ideas, Jacob (I couldn't find you in the @)?

Found the issue. Aliasing generic typings makes the alias itself generic and thus it uses implicit Any making the generic typing not work as intended. Furthermore, when importing just immutabledict, it does not like that pre-existing TypeVar's are used, instead favoring for the TypeVar's to be created in the typing if that makes sense. The necessary changes have been made and marked for review.

@jamart28 : Can you provide me with more context of that the aliases are and where they are being used?

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]"]
InstantiationTypes = (Mapping, Iterable) # pylint:disable=invalid-name
def immutabledict(
iterable: Optional[AllowableSourceType] = None, *, forbid_duplicate_keys: bool = False
) -> "ImmutableDict[KT, VT]":

Excuse if my wording of alias wasn't exactly right. That's all I could think to call it.

@jamart28 : ah, okay, so is the problem the type alias AllowableSourceType?

@jamart28 : python/mypy#606 implies type aliases with generics should work. Can you check what release of mypy it was added in vs. what release of mypy we are using?

@gabbard: That issue (and subsequent PR that allowed the generics) are from 2016 whereas the version we are using is from 2018. As such I think it's safe to say that the change is in the version we use. I believe that the issue comes in that we are importing it but am unsure how to workaround that (given that my solution I thought I had found causes issues in immutablecollections)

I should also clarify that the issue in the above code is that AllowableSourceType is technically a generic and needs types declared otherwise it gets an implicit Any. Using the KT and VT again we get the errors shown above.

@gabbard @lichmaster98 Looking through I believe our best course of action that will make the change the least noticeable in use of the library by others will be to take the implicit Any that is already on AllowableSourceType in the following line and make it explicit with AllowableSourceType[Any, Any]. Let me know if this fix is acceptable.

iterable: Optional[AllowableSourceType] = None, *, forbid_duplicate_keys: bool = False

@jamart28 From my understanding of the original issue using an explicit Any should be ok as we are trying to eliminate implicit ones. That would be my recommendation.