KEMBAR78
stubgen: properly convert overloaded functions by chadrik · Pull Request #9613 · python/mypy · GitHub
Skip to content

Conversation

@chadrik
Copy link
Contributor

@chadrik chadrik commented Oct 19, 2020

Description

Improves stubgen to convert functions that have been decorated with @overload. stubgen is useful not just for creating a starting point for manually authoring stubs, but also to create automated workflows to continuously regenerate stubs for projects that want to provide stubs to third parties to hide runtime complexity from mypy.

Test Plan

Added two new tests.

self.add_typing_import("Any")
annotation = ": {}".format(self.typing_name("Any"))
elif annotated_type and not is_self_arg:
elif annotated_type and not is_self_arg and not is_cls_arg:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

without this change I was getting an erroneous type for cls:

class A:
    @typing.overload
    @classmethod
    def g(cls: Any, x: typing.Tuple[int, int]) -> int: ...

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

Thanks, looks good! I only left one comment. This is ready to merge once it has been addressed.

mypy/stubgen.py Outdated
return is_abstract
elif (isinstance(expr.expr, NameExpr) and
(expr.expr.name == 'typing' or
self.import_tracker.reverse_alias.get('typing')) and
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this be expr.expr.name in ('typing', self.import_tracker.reverse_alias.get('typing'))? Maybe add test case that uses import typing as t or similar.

Copy link
Contributor Author

@chadrik chadrik Nov 13, 2020

Choose a reason for hiding this comment

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

Good catch. This was not working correctly for abc.abstractmethod|property, which is where I copied it from. I fixed it and added a test for both.

@chadrik chadrik force-pushed the stubgen-overload branch 2 times, most recently from d144d15 to c4a2ce4 Compare November 13, 2020 18:41
@chadrik
Copy link
Contributor Author

chadrik commented Nov 18, 2020

This should be all ready to merge

@chadrik
Copy link
Contributor Author

chadrik commented Jan 14, 2021

Hi, this is still ready to merge!

@JelleZijlstra JelleZijlstra merged commit 92923b2 into python:master Jan 15, 2021
@chadrik chadrik deleted the stubgen-overload branch August 22, 2022 18:59
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