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

Added type annotations to pyi interface file. #70

Merged
merged 28 commits into from
Apr 26, 2023

Conversation

gauravmm
Copy link
Contributor

Added type annotations for this excellent library!

This includes everything for frozendict and FrozenOrderedDict, but not the various helper methods.

Let me know if I can do anything to help.

gauravmm and others added 2 commits December 20, 2022 03:12
Mypy was complaining, so I added a few more methods.
self: Mapping[_KT, _VT], other: Mapping[_KT, _VT]
) -> "frozendict[_KT, _VT]": ...

class frozendict(Mapping[_KT, _VT], Generic[_KT, _VT]):
Copy link
Owner

Choose a reason for hiding this comment

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

Why Generic?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It tells the type system that the class itself supports two generic types.

Copy link
Owner

Choose a reason for hiding this comment

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

But Generic[T1, T2] means the class is generic and supports two parameters. This is not true, since the class is a map. There's also an example of custom dict on typing official docs, and it uses Mapping, not Generic.

def copy(self: Self) -> Self: ...
def __copy__(self: Self) -> Self: ...
def __deepcopy__(self: Self) -> Self: ...
# Omit __reduce__, its used for Pickle and we don't need the annotation in code.
Copy link
Owner

@Marco-Sulla Marco-Sulla Dec 20, 2022

Choose a reason for hiding this comment

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

? Well, also __copy__ is used for copy library, etc. Why pickle is different?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pickle needs a reference to the class as well. If we leave it out, the only people affected are those developing pickle.

Copy link
Owner

Choose a reason for hiding this comment

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

I do not understand. What's the difference with __copy__?

Copy link
Owner

Choose a reason for hiding this comment

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

Anyway I think I'll merge, but have you tested it? There's a bad prototype here: https://github.com/Marco-Sulla/python-frozendict/blob/master/test/typed.py

@Marco-Sulla
Copy link
Owner

Marco-Sulla commented Dec 20, 2022

Great job. A question: are you sure we need __init__, __getitem__, __len__, __iter__, __repr__ and __reversed__, since we inherit from Mapping[_KT, _VT]?

@@ -0,0 +1,54 @@
from collections.abc import Hashable
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like Hashable is imported but not actually used?

Copy link
Owner

Choose a reason for hiding this comment

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

This is highly possible.

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

Successfully merging this pull request may close these issues.

3 participants