-
Notifications
You must be signed in to change notification settings - Fork 363
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
base: master
Are you sure you want to change the base?
Conversation
fsspec/dircache.py
Outdated
type: str | ||
|
||
|
||
class DirCache(MutableMapping[str, List[DirEntry]]): |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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 object
s, not just the key type, hence the type ignore here.
There was a problem hiding this comment.
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 :|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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], |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
fsspec/dircache.py
Outdated
type: str | ||
|
||
|
||
class DirCache(MutableMapping[str, List[DirEntry]]): |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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 :|
"""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 |
There was a problem hiding this comment.
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.
Continuation of #1284, but add type hints to
fsspec.mapping
andfsspec.dircache
.