Skip to content
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

Stubgen: split @abstractproperty #8066

Merged
merged 2 commits into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 12 additions & 4 deletions mypy/stubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,11 +647,14 @@ def process_name_expr_decorator(self, expr: NameExpr, context: Decorator) -> boo
'asyncio.coroutines',
'types'):
self.add_coroutine_decorator(context.func, name, name)
elif any(self.refers_to_fullname(name, target)
for target in ('abc.abstractmethod', 'abc.abstractproperty')):
elif self.refers_to_fullname(name, 'abc.abstractmethod'):
self.add_decorator(name)
self.import_tracker.require_name(name)
is_abstract = True
elif self.refers_to_fullname(name, 'abc.abstractproperty'):
self.add_decorator('property')
self.add_decorator('abc.abstractmethod')
is_abstract = True
TH3CHARLie marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'd like to be more specific about here. (The previous commit has incorrect tests)
I intentionally did not require_name in import_tracker. This is because is_abstract will be set and abc will be imported, so directly add abc.abstractmethod would work. And as shown in testcase: testAbstractProperty3_semanal, if the imported abstractproperty is aliased, to properly split would have to modify code in import_tracker, which IMO is not necessary since using abc.abstractmethod already achieves purposes

return is_abstract

def refers_to_fullname(self, name: str, fullname: str) -> bool:
Expand All @@ -674,8 +677,13 @@ def process_member_expr_decorator(self, expr: MemberExpr, context: Decorator) ->
(expr.expr.name == 'abc' or
self.import_tracker.reverse_alias.get('abc')) and
expr.name in ('abstractmethod', 'abstractproperty')):
self.import_tracker.require_name(expr.expr.name)
self.add_decorator('%s.%s' % (expr.expr.name, expr.name))
if expr.name == 'abstractproperty':
self.import_tracker.require_name(expr.expr.name)
self.add_decorator('%s' % ('property'))
self.add_decorator('%s.%s' % (expr.expr.name, 'abstractmethod'))
else:
self.import_tracker.require_name(expr.expr.name)
self.add_decorator('%s.%s' % (expr.expr.name, expr.name))
is_abstract = True
elif expr.name == 'coroutine':
if (isinstance(expr.expr, MemberExpr) and
Expand Down
24 changes: 21 additions & 3 deletions test-data/unit/stubgen.test
Original file line number Diff line number Diff line change
Expand Up @@ -1625,7 +1625,8 @@ import abc
from typing import Any

class A(metaclass=abc.ABCMeta):
@abc.abstractproperty
@property
@abc.abstractmethod
def x(self) -> Any: ...

[case testAbstractProperty2_semanal]
Expand All @@ -1638,11 +1639,28 @@ class A:

[out]
import abc
from abc import abstractproperty
from typing import Any

class A(metaclass=abc.ABCMeta):
@abstractproperty
@property
@abc.abstractmethod
def x(self) -> Any: ...

[case testAbstractProperty3_semanal]
import other
from abc import abstractproperty as alias_name

class A:
@alias_name
def x(self): pass

[out]
import abc
from typing import Any

class A(metaclass=abc.ABCMeta):
@property
@abc.abstractmethod
def x(self) -> Any: ...

[case testClassWithNameAnyOrOptional]
Expand Down