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

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

Open
jamart28 opened this issue Feb 19, 2020 · 10 comments
Open

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

jamart28 opened this issue Feb 19, 2020 · 10 comments
Assignees

Comments

@jamart28
Copy link
Collaborator

jamart28 commented Feb 19, 2020

Implicit Any in immutabledict throws error on vistautils#86

Tracing problem through imports to find issue

@jamart28 jamart28 changed the title Implicit Any in immutabledict throws error on [vistautils#86](https://github.com/isi-vista/vistautils/issues/86) Implicit Any in immutabledict throws error on vistautils#86 Feb 19, 2020
@jamart28
Copy link
Collaborator Author

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 @)?

@jamart28
Copy link
Collaborator Author

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.

@gabbard
Copy link

gabbard commented Feb 19, 2020

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

@jamart28
Copy link
Collaborator Author

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.

@gabbard
Copy link

gabbard commented Feb 19, 2020

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

@gabbard
Copy link

gabbard commented Feb 19, 2020

@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?

@jamart28
Copy link
Collaborator Author

jamart28 commented Feb 19, 2020

@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)

@jamart28
Copy link
Collaborator Author

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.

@jamart28 jamart28 self-assigned this Feb 20, 2020
@jamart28
Copy link
Collaborator Author

jamart28 commented Feb 20, 2020

@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

@lichtefeld
Copy link
Collaborator

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants