-
Notifications
You must be signed in to change notification settings - Fork 200
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 type hints to base.py
#1302
base: typehints
Are you sure you want to change the base?
Conversation
Is there any task remaining for this PR? |
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 have some comments/suggestions.
Some of my comments apply in multiple places, but I only made the comment once. For example the kwargs type hint comment.
Note, I am not a maintainer of this project, but a contributor and user of it.
- annotations import in all files using typehints - typehint specific imports under type_checking condition - native library imports > installed libs > module import
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 have only done a quick review on GitHub. I haven't actually checked out this PR in my code editor to check.
plexapi/base.py
Outdated
from typing import (TYPE_CHECKING, Any, Callable, Dict, List, Optional, Set, | ||
Type, TypeVar, Union, cast, 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.
Use individual lines (I want to eventually use Black formatting). Similar other files.
from typing import (TYPE_CHECKING, Any, Callable, Dict, List, Optional, Set, | |
Type, TypeVar, Union, cast, overload) | |
from typing import ( | |
TYPE_CHECKING, | |
Any, | |
Callable, | |
Dict, | |
List, | |
Optional, | |
Set, | |
Type, | |
TypeVar, | |
Union, | |
cast, | |
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.
default config or have something in mind for this project?
- all single quotes will be converted to double unless "--skip-string-normalization" is passed
- all docstrings whitespaces will be stripped
- should a pyproject.toml be created for black config?
plexapi/base.py
Outdated
""" Builds the details key with the XML include parameters. | ||
All parameters are included by default with the option to override each parameter | ||
or disable each parameter individually by setting it to False or 0. | ||
""" | ||
details_key = self.key | ||
details_key = str(self.key) |
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.
If self.key is None
then str(None)
turns into 'None'
so you end up return 'None'
.
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.
good catch, removed casting
side effect: d4fc723#r1452965661
""" Returns True if this object is a child of the given attributes. | ||
This will search the parent objects all the way to the top. | ||
|
||
Parameters: | ||
**kwargs (dict): The attributes and values to search for in the parent objects. | ||
See all possible `**kwargs*` in :func:`~plexapi.base.PlexObject.fetchItem`. | ||
""" | ||
obj = self | ||
obj = self # ? Why is this needed? |
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.
So you don't mutate self
in the while
loop.
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.
isn't object address copied here? or am i missing something?
class Car:
def __init__(self, name: str, color: str) -> None:
self.name = name
self.color = color
def _dont_mutate_me(self) -> None:
obj = self # only memory address of self is copied not the object
obj.name = "foo" # will mutate self.name
assert id(obj) == id(self) # True
def __str__(self) -> str:
return f"I am a {self.color} {self.name}."
def main() -> None:
car = Car(name="Tesla", color="red")
print(car) # I am a red Tesla.
car._dont_mutate_me()
print(car) # I am a red foo.
if __name__ == "__main__":
main()
I also created a new branch for |
- fix _INCLUDES attr by including an empty dict - add explicit return types for methods missing them
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.
marked side effect of suggestion
if ekey is None: | ||
raise BadRequest('ekey was not provided') |
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.
Why is guard clause removed?
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.
def fetchItems(
self,
ekey: Union[str, List[int]],
cls: Optional[Type[PlexObjectT]] = None,
container_start: Optional[int] = None,
container_size: Optional[int] = None,
maxresults: Optional[int] = None,
**kwargs: Any,
):
ekey
is never None
as defined by the method, hence unnecessary check. could typehint to accept None
but then calling fetchItems
without passing ekey
does not make sense for the method, None
should be handled by the caller imo
if not data: | ||
return [] |
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.
Is this necessary?
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.
.query
can return None
and .findItems
does not accept None
. so it should be handled in some way. returned empty list as .fetchItems
returns a list
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.
on a second thought maybe returning result
is a better choice instead of empty list? or should raise error? what is the preferred way to handle no date? other choice is to make query raise an error instead of returning None
if data is None: | ||
raise NotFound(f'Unable to find elem: {key=}') |
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.
Is this necessary?
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.
as long as query could return None
, handling of None
should be done, it is done here as next step requires subscripting data
self._loadData(data[0]) # None is not subscriptable
self._server = server | ||
self._data = data | ||
self._initpath = initpath or self.key | ||
self._parent = weakref.ref(parent) if parent is not None else None | ||
self._details_key = None |
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.
Why is this removed?
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.
being assigned after few lines as below and not used before that assignment and this removal, hence the statement has no effect
self._details_key = self._buildDetailsKey()
if not data: | ||
raise NotFound(f'Unable to find elem: {key=}') |
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.
Is this necessary?
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.
for elem in data: # None not iterable
should I break this PR further down? Maybe on a per function basis? That should make it easier for review as currently this change is maybe so huge that it is daunting to start reviewing, and multiple threads going on for each change. |
I'm considering whether it would be more beneficial to merge it into
@JonnyWong16, I'd appreciate your thoughts on this. Do you think the benefits of direct merging outweigh any potential risks? either way I will close this PR and start anew since this is too huge an undertaking for review. |
Description
Support mypy and/or pyright for the
base.py
related #1296
Type of change
Please delete options that are not relevant.
Checklist: