Skip to content

Commit

Permalink
Stubgen: split @abstractproperty (#8066)
Browse files Browse the repository at this point in the history
Fixes #7952
  • Loading branch information
TH3CHARLie authored and msullivan committed Dec 11, 2019
1 parent 4fc7400 commit 63057d8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
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
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

0 comments on commit 63057d8

Please sign in to comment.