Skip to content

Commit

Permalink
Fix handling of optionals in mypy plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
dmontagu committed Mar 13, 2024
1 parent 18d39fe commit 748d46d
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions pydantic/mypy.py
Expand Up @@ -4,6 +4,7 @@

import sys
from configparser import ConfigParser
from contextlib import ExitStack
from typing import Any, Callable, Iterator

from mypy.errorcodes import ErrorCode
Expand Down Expand Up @@ -366,12 +367,10 @@ def expand_type(self, current_info: TypeInfo, api: SemanticAnalyzerPluginInterfa
# however this plugin is called very late, so all types should be fully ready.
# Also, it is tricky to avoid eager expansion of Self types here (e.g. because
# we serialize attributes).
expanded_type = expand_type(self.type, {self.info.self_type.id: fill_typevars(current_info)})
if isinstance(self.type, UnionType) and not isinstance(expanded_type, UnionType):
if not api.final_iteration:
raise _DeferAnalysis()
return expanded_type
return self.type
with ExitStack() as stack:
if MYPY_VERSION_TUPLE >= (1, 5):
stack.enter_context(state.strict_optional_set(api.options.strict_optional))
return expand_type(self.type, {self.info.self_type.id: fill_typevars(current_info)})

def to_var(self, current_info: TypeInfo, api: SemanticAnalyzerPluginInterface, use_alias: bool) -> Var:
"""Based on mypy.plugins.dataclasses.DataclassAttribute.to_var."""
Expand Down

0 comments on commit 748d46d

Please sign in to comment.