Skip to content

Commit

Permalink
chore: delete _infer_task_on_kart_attr_init_type
Browse files Browse the repository at this point in the history
  • Loading branch information
kitagry committed Oct 26, 2024
1 parent 4589525 commit 96d4d8e
Showing 1 changed file with 2 additions and 49 deletions.
51 changes: 2 additions & 49 deletions gokart/mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,23 @@
from typing import Callable, Final, Iterator, Literal, Optional

import luigi
from mypy.expandtype import expand_type, expand_type_by_instance
from mypy.expandtype import expand_type
from mypy.nodes import (
ARG_NAMED_OPT,
ARG_POS,
Argument,
AssignmentStmt,
Block,
CallExpr,
ClassDef,
Context,
EllipsisExpr,
Expression,
FuncDef,
IfStmt,
JsonDict,
MemberExpr,
NameExpr,
PlaceholderNode,
RefExpr,
Statement,
SymbolTableNode,
TempNode,
TypeInfo,
Var,
Expand All @@ -45,13 +41,11 @@
from mypy.typeops import map_type_from_supertype
from mypy.types import (
AnyType,
CallableType,
Instance,
NoneType,
Type,
TypeOfAny,
UnionType,
get_proper_type,
)
from mypy.typevars import fill_typevars

Expand Down Expand Up @@ -328,7 +322,7 @@ def collect_attributes(self) -> Optional[list[TaskOnKartAttribute]]:

current_attr_names.add(lhs.name)
with state.strict_optional_set(self._api.options.strict_optional):
init_type = self._infer_task_on_kart_attr_init_type(sym, stmt)
init_type = sym.type

# infer Parameter type
if init_type is None:
Expand Down Expand Up @@ -366,47 +360,6 @@ def _collect_parameter_args(self, expr: Expression) -> tuple[bool, dict[str, Exp
return True, args
return False, {}

def _infer_task_on_kart_attr_init_type(self, sym: SymbolTableNode, context: Context) -> Type | None:
"""Infer __init__ argument type for an attribute.
In particular, possibly use the signature of __set__.
"""
default = sym.type
if sym.implicit:
return default
t = get_proper_type(sym.type)

# Perform a simple-minded inference from the signature of __set__, if present.
# We can't use mypy.checkmember here, since this plugin runs before type checking.
# We only support some basic scanerios here, which is hopefully sufficient for
# the vast majority of use cases.
if not isinstance(t, Instance):
return default
setter = t.type.get('__set__')

if not setter:
return default

if isinstance(setter.node, FuncDef):
super_info = t.type.get_containing_type_info('__set__')
assert super_info
if setter.type:
setter_type = get_proper_type(map_type_from_supertype(setter.type, t.type, super_info))
else:
return AnyType(TypeOfAny.unannotated)
if isinstance(setter_type, CallableType) and setter_type.arg_kinds == [
ARG_POS,
ARG_POS,
ARG_POS,
]:
return expand_type_by_instance(setter_type.arg_types[2], t)
else:
self._api.fail(f'Unsupported signature for "__set__" in "{t.type.name}"', context)
else:
self._api.fail(f'Unsupported "__set__" in "{t.type.name}"', context)

return default

def _infer_type_from_parameters(self, parameter: Expression) -> Optional[Type]:
"""
Generate default type from Parameter.
Expand Down

0 comments on commit 96d4d8e

Please sign in to comment.