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

Add typehints to fsspec.mapping and fsspec.dircache #1396

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

alanhdu
Copy link
Contributor

@alanhdu alanhdu commented Oct 20, 2023

Continuation of #1284, but add type hints to fsspec.mapping and fsspec.dircache.

type: str


class DirCache(MutableMapping[str, List[DirEntry]]):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This class doesn't ever specify the key and value types, so theoretically I could make this a generic type. The docstrings imply this is the right type though?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, this is right

fsspec/dircache.py Outdated Show resolved Hide resolved
def __contains__(self, item: object) -> bool:
try:
self[item]
self[item] # type: ignore[index]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a little unfortunate -- the MutableMappign typehints say that __contrains__ should take all objects, not just the key type, hence the type ignore here.

Copy link
Member

Choose a reason for hiding this comment

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

I hate this kind of inconsistency :|

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I don't love it either.. it's not great.

Copy link
Contributor

Choose a reason for hiding this comment

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

You can around this by adding an assert isinstance here.


def getitems(
self,
keys: Iterable[str],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could keep this as a list if we want to add that restriction.

"""Remove all keys below root - empties out mapping"""
try:
self.fs.rm(self.root, True)
self.fs.mkdir(self.root)
except: # noqa: E722
pass

def getitems(self, keys, on_error="raise"):
@overload
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a bit of ugliness to make the output type of getitems dynamic based on on_error

Copy link
Member

Choose a reason for hiding this comment

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

Can we not just use str and ignore the overload? I don't think this kind of construct is worth it.

Copy link
Member

@martindurant martindurant left a comment

Choose a reason for hiding this comment

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

Almost everything is fine here.

type: str


class DirCache(MutableMapping[str, List[DirEntry]]):
Copy link
Member

Choose a reason for hiding this comment

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

Yes, this is right

def __contains__(self, item: object) -> bool:
try:
self[item]
self[item] # type: ignore[index]
Copy link
Member

Choose a reason for hiding this comment

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

I hate this kind of inconsistency :|

fsspec/dircache.py Outdated Show resolved Hide resolved
"""Remove all keys below root - empties out mapping"""
try:
self.fs.rm(self.root, True)
self.fs.mkdir(self.root)
except: # noqa: E722
pass

def getitems(self, keys, on_error="raise"):
@overload
Copy link
Member

Choose a reason for hiding this comment

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

Can we not just use str and ignore the overload? I don't think this kind of construct is worth it.

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