Skip to content

Commit 63057d8

Browse files
TH3CHARLiemsullivan
authored andcommitted
Stubgen: split @abstractproperty (python#8066)
Fixes python#7952
1 parent 4fc7400 commit 63057d8

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

mypy/stubgen.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -647,11 +647,14 @@ def process_name_expr_decorator(self, expr: NameExpr, context: Decorator) -> boo
647647
'asyncio.coroutines',
648648
'types'):
649649
self.add_coroutine_decorator(context.func, name, name)
650-
elif any(self.refers_to_fullname(name, target)
651-
for target in ('abc.abstractmethod', 'abc.abstractproperty')):
650+
elif self.refers_to_fullname(name, 'abc.abstractmethod'):
652651
self.add_decorator(name)
653652
self.import_tracker.require_name(name)
654653
is_abstract = True
654+
elif self.refers_to_fullname(name, 'abc.abstractproperty'):
655+
self.add_decorator('property')
656+
self.add_decorator('abc.abstractmethod')
657+
is_abstract = True
655658
return is_abstract
656659

657660
def refers_to_fullname(self, name: str, fullname: str) -> bool:
@@ -674,8 +677,13 @@ def process_member_expr_decorator(self, expr: MemberExpr, context: Decorator) ->
674677
(expr.expr.name == 'abc' or
675678
self.import_tracker.reverse_alias.get('abc')) and
676679
expr.name in ('abstractmethod', 'abstractproperty')):
677-
self.import_tracker.require_name(expr.expr.name)
678-
self.add_decorator('%s.%s' % (expr.expr.name, expr.name))
680+
if expr.name == 'abstractproperty':
681+
self.import_tracker.require_name(expr.expr.name)
682+
self.add_decorator('%s' % ('property'))
683+
self.add_decorator('%s.%s' % (expr.expr.name, 'abstractmethod'))
684+
else:
685+
self.import_tracker.require_name(expr.expr.name)
686+
self.add_decorator('%s.%s' % (expr.expr.name, expr.name))
679687
is_abstract = True
680688
elif expr.name == 'coroutine':
681689
if (isinstance(expr.expr, MemberExpr) and

test-data/unit/stubgen.test

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,7 +1625,8 @@ import abc
16251625
from typing import Any
16261626

16271627
class A(metaclass=abc.ABCMeta):
1628-
@abc.abstractproperty
1628+
@property
1629+
@abc.abstractmethod
16291630
def x(self) -> Any: ...
16301631

16311632
[case testAbstractProperty2_semanal]
@@ -1638,11 +1639,28 @@ class A:
16381639

16391640
[out]
16401641
import abc
1641-
from abc import abstractproperty
16421642
from typing import Any
16431643

16441644
class A(metaclass=abc.ABCMeta):
1645-
@abstractproperty
1645+
@property
1646+
@abc.abstractmethod
1647+
def x(self) -> Any: ...
1648+
1649+
[case testAbstractProperty3_semanal]
1650+
import other
1651+
from abc import abstractproperty as alias_name
1652+
1653+
class A:
1654+
@alias_name
1655+
def x(self): pass
1656+
1657+
[out]
1658+
import abc
1659+
from typing import Any
1660+
1661+
class A(metaclass=abc.ABCMeta):
1662+
@property
1663+
@abc.abstractmethod
16461664
def x(self) -> Any: ...
16471665

16481666
[case testClassWithNameAnyOrOptional]

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy