Skip to content

Fix a crash on mypy master branch #2670

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

Merged
merged 2 commits into from
May 9, 2025
Merged
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
13 changes: 12 additions & 1 deletion mypy_django_plugin/transformers/functional.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
from typing import Any

from mypy.checkmember import analyze_member_access
from mypy.errorcodes import ATTR_DEFINED
from mypy.nodes import CallExpr, MemberExpr
from mypy.plugin import AttributeContext
from mypy.types import AnyType, Instance, TypeOfAny
from mypy.types import Type as MypyType
from mypy.version import __version__ as mypy_version

from mypy_django_plugin.lib import helpers

mypy_version_info = tuple(map(int, mypy_version.partition("+")[0].split(".")))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to a version-based check. This is somewhat ugly, but I didn't want to add packaging as a dependency.



def resolve_str_promise_attribute(ctx: AttributeContext) -> MypyType:
if isinstance(ctx.context, MemberExpr):
Expand All @@ -20,6 +25,12 @@ def resolve_str_promise_attribute(ctx: AttributeContext) -> MypyType:
str_info = helpers.lookup_fully_qualified_typeinfo(helpers.get_typechecker_api(ctx), "builtins.str")
assert str_info is not None
str_type = Instance(str_info, [])

# TODO: [mypy 1.16+] Remove this workaround for passing `msg` to `analyze_member_access()`.
extra: dict[str, Any] = {}
if mypy_version_info < (1, 16):
extra["msg"] = ctx.api.msg

return analyze_member_access(
method_name,
str_type,
Expand All @@ -28,7 +39,7 @@ def resolve_str_promise_attribute(ctx: AttributeContext) -> MypyType:
is_super=False,
# operators are already handled with magic methods defined in the stubs for _StrPromise
is_operator=False,
msg=ctx.api.msg,
original_type=ctx.type,
chk=helpers.get_typechecker_api(ctx),
**extra,
)
Loading