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

Fix dataclass plugin to work with new semantic analyzer #6515

Merged
merged 3 commits into from
Mar 7, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions mypy/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,15 @@ def qualified_name(self, n: str) -> str:
"""Make qualified name using current module and enclosing class (if any)."""
raise NotImplementedError

@abstractmethod
def defer(self) -> None:
"""Call this to defer the processing of the current node.

This will request an additional iteration of semantic analysis.
Only available with new semantic analyzer.
"""
raise NotImplementedError


# A context for a function hook that infers the return type of a function with
# a special signature.
Expand Down
24 changes: 21 additions & 3 deletions mypy/plugins/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from mypy.nodes import (
ARG_OPT, ARG_POS, MDEF, Argument, AssignmentStmt, CallExpr,
Context, Expression, FuncDef, JsonDict, NameExpr,
SymbolTableNode, TempNode, TypeInfo, Var,
SymbolTableNode, TempNode, TypeInfo, Var, TypeVarExpr
)
from mypy.plugin import ClassDefContext
from mypy.plugins.common import add_method, _get_decorator_bool_argument
Expand All @@ -21,6 +21,8 @@
'dataclasses.dataclass',
} # type: Final

SELF_TVAR_NAME = '_DT' # type: Final


class DataclassAttribute:
def __init__(
Expand Down Expand Up @@ -77,6 +79,12 @@ def transform(self) -> None:
ctx = self._ctx
info = self._ctx.cls.info
attributes = self.collect_attributes()
if ctx.api.options.new_semantic_analyzer:
# Check if attribute types are ready.
for attr in attributes:
if info[attr.name].type is None:
ctx.api.defer()
return
decorator_arguments = {
'init': _get_decorator_bool_argument(self._ctx, 'init', True),
'eq': _get_decorator_bool_argument(self._ctx, 'eq', True),
Expand All @@ -92,14 +100,23 @@ def transform(self) -> None:
return_type=NoneTyp(),
)

if (decorator_arguments['eq'] and info.get('__eq__') is None or
decorator_arguments['order']):
# Type variable for self types in generated methods.
obj_type = ctx.api.named_type('__builtins__.object')
self_tvar_expr = TypeVarExpr(SELF_TVAR_NAME, info.fullname() + '.' + SELF_TVAR_NAME,
[], obj_type)
info.names[SELF_TVAR_NAME] = SymbolTableNode(MDEF, self_tvar_expr)

# Add an eq method, but only if the class doesn't already have one.
if decorator_arguments['eq'] and info.get('__eq__') is None:
for method_name in ['__eq__', '__ne__']:
# The TVar is used to enforce that "other" must have
# the same type as self (covariant). Note the
# "self_type" parameter to add_method.
obj_type = ctx.api.named_type('__builtins__.object')
cmp_tvar_def = TypeVarDef('T', 'T', -1, [], obj_type)
cmp_tvar_def = TypeVarDef(SELF_TVAR_NAME, info.fullname() + '.' + SELF_TVAR_NAME,
-1, [], obj_type)
cmp_other_type = TypeVarType(cmp_tvar_def)
cmp_return_type = ctx.api.named_type('__builtins__.bool')

Expand All @@ -121,7 +138,8 @@ def transform(self) -> None:
# Like for __eq__ and __ne__, we want "other" to match
# the self type.
obj_type = ctx.api.named_type('__builtins__.object')
order_tvar_def = TypeVarDef('T', 'T', -1, [], obj_type)
order_tvar_def = TypeVarDef(SELF_TVAR_NAME, info.fullname() + '.' + SELF_TVAR_NAME,
-1, [], obj_type)
order_other_type = TypeVarType(order_tvar_def)
order_return_type = ctx.api.named_type('__builtins__.bool')
order_args = [
Expand Down
4 changes: 4 additions & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3818,6 +3818,10 @@ def add_symbol_table_node(self, name: str, stnode: SymbolTableNode) -> None:
else:
self.globals[name] = stnode

def defer(self) -> None:
assert not self.options.new_semantic_analyzer
raise NotImplementedError('This is only available with --new-semantic-analyzer')


def replace_implicit_first_type(sig: FunctionLike, new: Type) -> FunctionLike:
if isinstance(sig, CallableType):
Expand Down
2 changes: 0 additions & 2 deletions mypy/test/hacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
new_semanal_blacklist = [
'check-async-await.test',
'check-classes.test',
'check-custom-plugin.test',
'check-dataclasses.test',
'check-expressions.test',
'check-flags.test',
'check-functions.test',
Expand Down
12 changes: 7 additions & 5 deletions test-data/unit/check-custom-plugin.test
Original file line number Diff line number Diff line change
Expand Up @@ -491,16 +491,18 @@ class Instr(Generic[T]): ...
plugins=<ROOT>/test-data/unit/plugins/dyn_class.py

[case testDynamicClassPluginNegatives]
# flags: --config-file tmp/mypy.ini
# flags: --new-semantic-analyzer --config-file tmp/mypy.ini
from mod import declarative_base, Column, Instr, non_declarative_base

Bad1 = non_declarative_base()
Bad2 = Bad3 = declarative_base()

class C1(Bad1): ... # E: Invalid base class
class C2(Bad2): ... # E: Invalid base class
class C3(Bad3): ... # E: Invalid base class

class C1(Bad1): ... # E: Invalid base class \
# E: Invalid type "__main__.Bad1"
class C2(Bad2): ... # E: Invalid base class \
# E: Invalid type "__main__.Bad2"
class C3(Bad3): ... # E: Invalid base class \
# E: Invalid type "__main__.Bad3"
[file mod.py]
from typing import Generic, TypeVar
def declarative_base(): ...
Expand Down
3 changes: 2 additions & 1 deletion test-data/unit/check-dataclasses.test
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,9 @@ class Application:

[builtins fixtures/list.pyi]

-- Blocked by #6454
[case testDataclassOrderingWithCustomMethods]
# flags: --python-version 3.6
# flags: --python-version 3.6 --no-new-semantic-analyzer
from dataclasses import dataclass

@dataclass(order=True)
Expand Down
8 changes: 4 additions & 4 deletions test-data/unit/check-incremental.test
Original file line number Diff line number Diff line change
Expand Up @@ -3864,10 +3864,10 @@ class A:
tmp/b.py:3: error: Revealed type is 'def (a: builtins.int) -> a.A'
tmp/b.py:4: error: Revealed type is 'def (builtins.object, builtins.object) -> builtins.bool'
tmp/b.py:5: error: Revealed type is 'def (builtins.object, builtins.object) -> builtins.bool'
tmp/b.py:6: error: Revealed type is 'def [T] (self: T`-1, other: T`-1) -> builtins.bool'
tmp/b.py:7: error: Revealed type is 'def [T] (self: T`-1, other: T`-1) -> builtins.bool'
tmp/b.py:8: error: Revealed type is 'def [T] (self: T`-1, other: T`-1) -> builtins.bool'
tmp/b.py:9: error: Revealed type is 'def [T] (self: T`-1, other: T`-1) -> builtins.bool'
tmp/b.py:6: error: Revealed type is 'def [_DT] (self: _DT`-1, other: _DT`-1) -> builtins.bool'
tmp/b.py:7: error: Revealed type is 'def [_DT] (self: _DT`-1, other: _DT`-1) -> builtins.bool'
tmp/b.py:8: error: Revealed type is 'def [_DT] (self: _DT`-1, other: _DT`-1) -> builtins.bool'
tmp/b.py:9: error: Revealed type is 'def [_DT] (self: _DT`-1, other: _DT`-1) -> builtins.bool'
tmp/b.py:18: error: Unsupported operand types for < ("A" and "int")
tmp/b.py:19: error: Unsupported operand types for <= ("A" and "int")
tmp/b.py:20: error: Unsupported operand types for > ("A" and "int")
Expand Down
1 change: 1 addition & 0 deletions test-data/unit/deps.test
Original file line number Diff line number Diff line change
Expand Up @@ -1400,6 +1400,7 @@ class B(A):

[out]
<m.A.(abstract)> -> <m.B.__init__>, m
<m.A._DT> -> <m.B._DT>
<m.A.__eq__> -> <m.B.__eq__>
<m.A.__init__> -> <m.B.__init__>, m.B.__init__
<m.A.__ne__> -> <m.B.__ne__>
Expand Down