Skip to content

Commit b0236f1

Browse files
committed
Add logic to support mypy 1.16.0
1 parent c3d2d00 commit b0236f1

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

returns/contrib/mypy/_features/kind.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from collections.abc import Sequence
22
from enum import Enum, unique
3+
from typing import Any
34

45
from mypy.checkmember import analyze_member_access
56
from mypy.plugin import (
@@ -21,6 +22,7 @@
2122
get_proper_type,
2223
)
2324
from mypy.types import Type as MypyType
25+
from mypy.version import __version__ as mypy_version
2426

2527
from returns.contrib.mypy._typeops.fallback import asserts_fallback_to_any
2628
from returns.contrib.mypy._typeops.visitor import translate_kind_instance
@@ -62,17 +64,24 @@ def attribute_access(ctx: AttributeContext) -> MypyType:
6264
return ctx.default_attr_type
6365

6466
exprchecker = ctx.api.expr_checker # type: ignore
67+
mypy_version_tuple = tuple(
68+
map(int, mypy_version.partition('+')[0].split('.'))
69+
)
70+
71+
extra_kwargs: dict[str, Any] = {}
72+
if mypy_version_tuple < (1, 16):
73+
extra_kwargs['msg'] = ctx.api.msg
6574
return analyze_member_access(
6675
ctx.context.name, # type: ignore
6776
accessed,
6877
ctx.context,
6978
is_lvalue=False,
7079
is_super=False,
7180
is_operator=False,
72-
msg=ctx.api.msg,
7381
original_type=instance,
7482
chk=ctx.api, # type: ignore
7583
in_literal_context=exprchecker.is_literal_context(),
84+
**extra_kwargs,
7685
)
7786

7887

returns/contrib/mypy/_typeops/analtype.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
from collections.abc import Sequence
12
from types import MappingProxyType
2-
from typing import Final, Literal, overload
3+
from typing import Any, Final, Literal, overload
34

45
from mypy.checkmember import analyze_member_access
56
from mypy.nodes import ARG_NAMED, ARG_OPT
67
from mypy.types import CallableType, FunctionLike, ProperType, get_proper_type
78
from mypy.types import Type as MypyType
9+
from mypy.version import __version__ as mypy_version
810

911
from returns.contrib.mypy._structures.args import FuncArg
1012
from returns.contrib.mypy._structures.types import CallableContext
@@ -21,7 +23,7 @@
2123
@overload
2224
def analyze_call(
2325
function: FunctionLike,
24-
args: list[FuncArg],
26+
args: Sequence[FuncArg],
2527
ctx: CallableContext,
2628
*,
2729
show_errors: Literal[True],
@@ -31,14 +33,20 @@ def analyze_call(
3133
@overload
3234
def analyze_call(
3335
function: FunctionLike,
34-
args: list[FuncArg],
36+
args: Sequence[FuncArg],
3537
ctx: CallableContext,
3638
*,
3739
show_errors: bool,
3840
) -> CallableType | None: ...
3941

4042

41-
def analyze_call(function, args, ctx, *, show_errors):
43+
def analyze_call(
44+
function: FunctionLike,
45+
args: Sequence[FuncArg],
46+
ctx: CallableContext,
47+
*,
48+
show_errors: bool,
49+
) -> CallableType | None:
4250
"""
4351
Analyzes function call based on passed arguments.
4452
@@ -48,7 +56,7 @@ def analyze_call(function, args, ctx, *, show_errors):
4856
We also allow to return ``None`` instead of showing errors.
4957
This might be helpful for cases when we run intermediate analysis.
5058
"""
51-
checker = ctx.api.expr_checker
59+
checker = ctx.api.expr_checker # type: ignore[attr-defined]
5260
with checker.msg.filter_errors(save_filtered_errors=True) as local_errors:
5361
_return_type, checked_function = checker.check_call(
5462
function,
@@ -63,7 +71,7 @@ def analyze_call(function, args, ctx, *, show_errors):
6371

6472
checker.msg.add_errors(local_errors.filtered_errors()) # noqa: WPS441
6573

66-
return checked_function
74+
return checked_function # type: ignore[no-any-return]
6775

6876

6977
def safe_translate_to_function(
@@ -110,6 +118,15 @@ def translate_to_function(
110118
This also preserves all type arguments as-is.
111119
"""
112120
checker = ctx.api.expr_checker # type: ignore
121+
122+
mypy_version_tuple = tuple(
123+
map(int, mypy_version.partition('+')[0].split('.'))
124+
)
125+
126+
extra_kwargs: dict[str, Any] = {}
127+
if mypy_version_tuple > (1, 16):
128+
extra_kwargs['msg'] = checker.msg
129+
113130
return get_proper_type(
114131
analyze_member_access(
115132
'__call__',
@@ -118,9 +135,8 @@ def translate_to_function(
118135
is_lvalue=False,
119136
is_super=False,
120137
is_operator=True,
121-
msg=checker.msg,
122138
original_type=function_def,
123139
chk=checker.chk,
124-
in_literal_context=checker.is_literal_context(),
140+
**extra_kwargs,
125141
)
126142
)

tests/test_contrib/test_hypothesis/test_laws/test_custom_strategy_for_callable.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ def do_nothing(
120120
def _callable_strategy(
121121
arg1: type[object], arg2: type[object]
122122
) -> StrategyFactory[Callable]:
123-
type_arg1 = int if arg1 == Any else arg1 # type: ignore[comparison-overlap]
124-
type_arg2 = int if arg2 == Any else arg2 # type: ignore[comparison-overlap]
123+
type_arg1 = int if arg1 == Any else arg1
124+
type_arg2 = int if arg2 == Any else arg2
125125
return_results = st.functions(
126126
pure=True,
127127
returns=strategy_from_container(Result)(Result[type_arg1, type_arg2]), # type: ignore[valid-type]

0 commit comments

Comments
 (0)