Skip to content

Fix strict optional handling in attrs plugin #17451

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 1 commit into from
Jun 29, 2024
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
17 changes: 16 additions & 1 deletion mypy/plugins/attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
deserialize_and_fixup_type,
)
from mypy.server.trigger import make_wildcard_trigger
from mypy.state import state
from mypy.typeops import get_type_vars, make_simplified_union, map_type_from_supertype
from mypy.types import (
AnyType,
Expand Down Expand Up @@ -317,9 +318,23 @@ def attr_class_maker_callback(

See https://www.attrs.org/en/stable/how-does-it-work.html for information on how attrs works.

If this returns False, some required metadata was not ready yet and we need another
If this returns False, some required metadata was not ready yet, and we need another
pass.
"""
with state.strict_optional_set(ctx.api.options.strict_optional):
# This hook is called during semantic analysis, but it uses a bunch of
# type-checking ops, so it needs the strict optional set properly.
return attr_class_maker_callback_impl(
ctx, auto_attribs_default, frozen_default, slots_default
)


def attr_class_maker_callback_impl(
ctx: mypy.plugin.ClassDefContext,
auto_attribs_default: bool | None,
frozen_default: bool,
slots_default: bool,
) -> bool:
info = ctx.cls.info

init = _get_decorator_bool_argument(ctx, "init", True)
Expand Down
20 changes: 20 additions & 0 deletions test-data/unit/check-plugin-attrs.test
Original file line number Diff line number Diff line change
Expand Up @@ -2475,3 +2475,23 @@ class B:
reveal_type(B.__hash__) # N: Revealed type is "None"

[builtins fixtures/plugin_attrs.pyi]

[case testAttrsStrictOptionalSetProperly]
from typing import Generic, Optional, TypeVar

import attr

T = TypeVar("T")

@attr.mutable()
class Parent(Generic[T]):
run_type: Optional[int] = None

@attr.mutable()
class Child(Parent[float]):
pass

Parent(run_type = None)
c = Child(run_type = None)
reveal_type(c.run_type) # N: Revealed type is "Union[builtins.int, None]"
[builtins fixtures/plugin_attrs.pyi]
Loading