Skip to content

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

Merged
merged 3 commits into from
Sep 28, 2022
Merged

Conversation

pmeier
Copy link
Collaborator

@pmeier pmeier commented Sep 27, 2022

This fixes the ongoing mypy CI failure caused by yesterdays 0.981 release

@@ -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]:
Copy link
Member

@NicolasHug NicolasHug Sep 27, 2022

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?

Copy link
Collaborator Author

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

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("."))
Copy link
Member

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?

Copy link
Collaborator Author

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.

@NicolasHug
Copy link
Member

BTW do the changes still pass with older versions of mypy?

@pmeier
Copy link
Collaborator Author

pmeier commented Sep 27, 2022

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 mypy didn't flag them yet. The new annotations should be correct and thus older versions should also accept them.

Edit: I checked for mypy==0.900, which is over a year old, and patch is still accepted. We should be ok.

Copy link
Contributor

@datumbox datumbox left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@pmeier pmeier merged commit 2d92728 into pytorch:main Sep 28, 2022
@pmeier pmeier deleted the mypy-0.981 branch September 28, 2022 05:44
facebook-github-bot pushed a commit that referenced this pull request Sep 29, 2022
Reviewed By: YosuaMichael

Differential Revision: D39885431

fbshipit-source-id: a5b82afc1a54e78c50f36fadc0a2f9bd977edcb6
@frgfm frgfm mentioned this pull request Oct 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants