mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-06 14:04:26 +08:00
Get inheritance of dataclass right
This commit is contained in:
@@ -6,7 +6,7 @@ Changelog
|
||||
0.14.2 (XXXX-XX-XX)
|
||||
+++++++++++++++++++
|
||||
|
||||
- Better support for enum completions
|
||||
- Better support for enums/dataclasses
|
||||
|
||||
|
||||
0.14.1 (2019-07-13)
|
||||
|
||||
@@ -24,6 +24,7 @@ from jedi.evaluate.base_context import ContextualizedNode, \
|
||||
NO_CONTEXTS, ContextSet, ContextWrapper, LazyContextWrapper
|
||||
from jedi.evaluate.context import ClassContext, ModuleContext, \
|
||||
FunctionExecutionContext
|
||||
from jedi.evaluate.context.klass import ClassMixin
|
||||
from jedi.evaluate.context import iterable
|
||||
from jedi.evaluate.lazy_context import LazyTreeContext, LazyKnownContext, \
|
||||
LazyKnownContexts
|
||||
@@ -530,11 +531,11 @@ def _dataclass(obj, arguments):
|
||||
return NO_CONTEXTS
|
||||
|
||||
|
||||
class DataclassWrapper(ContextWrapper):
|
||||
class DataclassWrapper(ContextWrapper, ClassMixin):
|
||||
def get_signatures(self):
|
||||
param_names = []
|
||||
for cls in reversed(list(self._wrapped_context.py__mro__())):
|
||||
if isinstance(cls, ClassContext) and not cls.is_stub():
|
||||
for cls in reversed(list(self.py__mro__())):
|
||||
if isinstance(cls, DataclassWrapper):
|
||||
filter_ = cls.get_global_filter()
|
||||
for name in filter_.values():
|
||||
d = name.tree_name.get_definition()
|
||||
|
||||
@@ -64,18 +64,34 @@ def test_pow_signature(Script):
|
||||
'pow(x: int, y: int, /) -> Any'}
|
||||
|
||||
|
||||
@pytest.mark.parametrize('decorator', ['@dataclass', '@dataclass(eq=True)'])
|
||||
def test_dataclass_signature(Script, skip_pre_python37, decorator):
|
||||
@pytest.mark.parametrize(
|
||||
'start, start_params', [
|
||||
['@dataclass\nclass X:', []],
|
||||
['@dataclass(eq=True)\nclass X:', []],
|
||||
[dedent('''
|
||||
class Y():
|
||||
y: int
|
||||
@dataclass
|
||||
class X(Y):'''), []],
|
||||
[dedent('''
|
||||
@dataclass
|
||||
class Y():
|
||||
y: int
|
||||
z = 5
|
||||
@dataclass
|
||||
class X(Y):'''), ['y']],
|
||||
]
|
||||
)
|
||||
def test_dataclass_signature(Script, skip_pre_python37, start, start_params):
|
||||
code = dedent('''
|
||||
class InventoryItem:
|
||||
name: str
|
||||
foo = 3
|
||||
unit_price: float
|
||||
quantity_on_hand: int = 0
|
||||
|
||||
InventoryItem(''')
|
||||
X(''')
|
||||
|
||||
code = 'from dataclasses import dataclass\n' + decorator + code
|
||||
code = 'from dataclasses import dataclass\n' + start + code
|
||||
|
||||
sig, = Script(code).call_signatures()
|
||||
assert [p.name for p in sig.params] == ['name', 'unit_price', 'quantity_on_hand']
|
||||
assert [p.name for p in sig.params] == start_params + ['name', 'unit_price', 'quantity_on_hand']
|
||||
|
||||
Reference in New Issue
Block a user