-
Notifications
You must be signed in to change notification settings - Fork 7.1k
fix mypy errors after the 0.981 release #6652
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
Conversation
@@ -199,7 +196,7 @@ def list_models(module: Optional[ModuleType] = None) -> List[str]: | |||
return sorted(models) | |||
|
|||
|
|||
def get_model_builder(name: str) -> Callable[..., M]: | |||
def get_model_builder(name: str) -> Callable[..., nn.Module]: |
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.
Out of curiosity why do we need to change this one, and not all the other usages of M
e.g. in register_model()
above?
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.
TypeVar
is used to create a "link" between parameters. For example I can do something like
def do_nothing(foo: M) -> M:
return foo
This tells mypy
"whatever we have as input type, we return as output type as well". This is useful in case the function can handle multiple types, e.g.
T = TypeVar("T", int, float)
def add(a: T, b: T) -> T:
return a + b
In the case above, there is no input with the type M
and so mypy
complains that it can't figure out the M
in the output.
Correct me if I'm wrong @datumbox, but I guess the annotation was intended to mean "we return any kind of model" here. Since M
is defined as
vision/torchvision/models/_api.py
Line 168 in 06d9726
M = TypeVar("M", bound=nn.Module) |
we can simply use nn.Module
here.
def _path_attribute_accessor(path: pathlib.Path, *, name: str) -> D: | ||
return cast(D, _getattr_closure(path, attrs=name.split("."))) | ||
def _path_attribute_accessor(path: pathlib.Path, *, name: str) -> Any: | ||
return _getattr_closure(path, attrs=name.split(".")) |
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 change needed?
Do you remember why we annotated the return with D
in the first place?
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 change needed?
Same as above. We no longer can use a TypeVar
as return annotation if we don't have any input with the same type.
Do you remember why we annotated the return with
D
in the first place?
IIRC, this is a remnant when this function was a local one inside
def path_accessor(getter: Union[str, Callable[[pathlib.Path], D]]) -> Callable[[Tuple[str, Any]], D]: |
In there the annotation was correct and ok, since we have a D
in the input parameters. At some point we factored them out since torchdata
doesn't like local functions, and I probably forgot to fix this since mypy
didn't complain.
BTW do the changes still pass with older versions of mypy? |
Most likely, but I'll check. The annotations were wrong to begin with, but older versions of Edit: I checked for |
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.
LGTM, thanks!
Reviewed By: YosuaMichael Differential Revision: D39885431 fbshipit-source-id: a5b82afc1a54e78c50f36fadc0a2f9bd977edcb6
This fixes the ongoing
mypy
CI failure caused by yesterdays0.981
release