GeezerVison is showing github.com — Update TypedDictType.__init__ signature to preserve backward compat by JukkaL · Pull Request #21590 · python/mypy
Update TypedDictType.__init__ signature to preserve backward compat
#21590
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
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter
Filter by extension
.py
(12)
All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
Update TypedDictType.__init__ signature to preserve backward compat
The new signature added a new parameter in the middle of the signature,
which may break plugins that use positional arguments. Add the
new parameter to the end of the parameter list so that existing
calls will continue to work. This looks a bit ugly, but backward
compatibility is the priority here.
Loading branch information
Uh oh!
There was an error while loading. Please reload this page.
commit 02888d0b9323324d9db7ce82a9ecf29567656ca1
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -107,7 +107,10 @@ def visit_tuple_type(self, t: TupleType) -> ProperType:
def visit_typeddict_type(self, t: TypedDictType) -> ProperType:
return self.copy_common(
t, TypedDictType(t.items, t.required_keys, t.readonly_keys, t.is_closed, t.fallback)
t,
TypedDictType(
t.items, t.required_keys, t.readonly_keys, t.fallback, is_closed=t.is_closed
),
)
def visit_literal_type(self, t: LiteralType) -> ProperType:
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -350,7 +350,7 @@ def _possible_callable_kwargs(cls, repl: Parameters, dict_type: Instance) -> Pro
return Instance(dict_type.type, [dict_type.args[0], extra_items])
# TODO: when PEP 728 `extra_items` is implemented, pass extra_items below.
is_closed = extra_items is None
return TypedDictType(kwargs, required_names, set(), is_closed, fallback=dict_type )
return TypedDictType(kwargs, required_names, set(), dict_type, is_closed=is_closed )
def visit_type_var_tuple(self, t: TypeVarTupleType) -> Type:
# Sometimes solver may need to expand a type variable with (a copy of) itself
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -277,7 +277,7 @@ def expr_to_unanalyzed_type(
value, options, allow_new_syntax, expr, lookup_qualified=lookup_qualified
)
result = TypedDictType(
items, set(), set(), False, Instance(MISSING_FALLBACK, ()), expr.line, expr.column
items, set(), set(), Instance(MISSING_FALLBACK, ()), expr.line, expr.column
)
result.extra_items_from = extra_items_from
return result
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -2143,7 +2143,7 @@ def visit_Dict(self, n: ast3.Dict) -> Type:
continue
return self.invalid_type(n)
items[item_name.value] = self.visit(value)
result = TypedDictType(items, set(), set(), False, _dummy_fallback, n.lineno, n.col_offset)
result = TypedDictType(items, set(), set(), _dummy_fallback, n.lineno, n.col_offset)
result.extra_items_from = extra_items_from
return result
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -663,7 +663,9 @@ def visit_typeddict_type(self, t: TypedDictType) -> ProperType:
fallback = self.s.create_anonymous_fallback()
is_closed = self.s.is_closed and t.is_closed
return TypedDictType(items, required_keys, readonly_keys, is_closed, fallback)
return TypedDictType(
items, required_keys, readonly_keys, fallback, is_closed=is_closed
)
elif isinstance(self.s, Instance):
return join_types(self.s, t.fallback)
else:
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -1186,7 +1186,9 @@ def visit_typeddict_type(self, t: TypedDictType) -> ProperType:
fallback = self.s.create_anonymous_fallback()
required_keys = self.s.required_keys | t.required_keys
return TypedDictType(items, required_keys, readonly_keys, is_closed, fallback)
return TypedDictType(
items, required_keys, readonly_keys, fallback, is_closed=is_closed
)
elif isinstance(self.s, Instance) and is_subtype(t, self.s):
return t
else:
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -926,7 +926,7 @@ def read_type(state: State, data: ReadBuffer) -> Type:
extra_items_from.append(val)
else:
td_items[key] = val
typeddict_type = TypedDictType(td_items, set(), set(), False, _dummy_fallback)
typeddict_type = TypedDictType(td_items, set(), set(), _dummy_fallback)
typeddict_type.extra_items_from = extra_items_from
read_loc(data, typeddict_type)
expect_end_tag(data)
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -785,7 +785,7 @@ def build_typeddict_typeinfo(
assert fallback is not None
info = existing_info or self.api.basic_new_typeinfo(name, fallback, line)
typeddict_type = TypedDictType(
item_types, required_keys, readonly_keys, is_closed, fallback
item_types, required_keys, readonly_keys, fallback, is_closed=is_closed
)
any_placeholder = has_placeholder(typeddict_type)
if typeddict_data:
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -39,6 +39,7 @@
ProperType,
TupleType,
Type,
TypedDictType,
TypeOfAny,
TypeType,
TypeVarId,
Expand Down
Expand Up
@@ -223,6 +224,22 @@ def test_indirection_no_infinite_recursion(self) -> None:
modules = visitor.modules
assert modules == {"__main__", "builtins"}
def test_typeddict_type_constructor_signature(self) -> None:
typ = TypedDictType({"x": self.fx.o}, {"x"}, set(), self.fx.a, 10, 20)
assert typ.fallback is self.fx.a
assert_equal(typ.line, 10)
assert_equal(typ.column, 20)
assert not typ.is_closed
closed = TypedDictType({"x": self.fx.o}, {"x"}, set(), self.fx.a, is_closed=True)
assert closed.is_closed
with self.assertRaises(TypeError):
TypedDictType( # type: ignore[call-arg]
{"x": self.fx.o}, {"x"}, set(), self.fx.a, 10, 20, True
)
class TypeOpsSuite(Suite):
def setUp(self) -> None:
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -284,11 +284,11 @@ def visit_typeddict_type(self, t: TypedDictType, /) -> Type:
items,
t.required_keys,
t.readonly_keys,
t.is_closed,
# TODO: This appears to be unsafe.
cast(Any, t.fallback.accept(self)),
t.line,
t.column,
is_closed=t.is_closed,
)
self.set_cached(t, result)
return result
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -1416,7 +1416,7 @@ def visit_typeddict_type(self, t: TypedDictType) -> Type:
fallback = t.fallback
is_closed = t.is_closed
return TypedDictType(
items, required_keys, readonly_keys, is_closed, fallback, t.line, t.column
items, required_keys, readonly_keys, fallback, t.line, t.column, is_closed=is_closed
)
def visit_raw_expression_type(self, t: RawExpressionType) -> Type:
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -3048,10 +3048,11 @@ def __init__(
items: dict[str, Type],
required_keys: set[str],
readonly_keys: set[str],
is_closed: bool,
fallback: Instance,
line: int = -1,
column: int = -1,
*,
is_closed: bool = False,
) -> None:
super().__init__(line, column)
self.items = items
Expand Down
Expand Up
@@ -3112,8 +3113,8 @@ def deserialize(cls, data: JsonDict) -> TypedDictType:
{n: deserialize_type(t) for (n, t) in data["items"]},
set(data["required_keys"]),
set(data["readonly_keys"]),
bool(data["is_closed"]),
Instance.deserialize(data["fallback"]),
is_closed=bool(data["is_closed"]),
)
def write(self, data: WriteBuffer) -> None:
Expand All
@@ -3133,8 +3134,8 @@ def read(cls, data: ReadBuffer) -> TypedDictType:
read_type_map(data),
set(read_str_list(data)),
set(read_str_list(data)),
read_bool(data),
fallback,
is_closed=read_bool(data),
)
assert read_tag(data) == END_TAG
return ret
Expand Down
Expand Up
@@ -3178,7 +3179,13 @@ def copy_modified(
items = {k: v for (k, v) in items.items() if k in item_names}
required_keys &= set(item_names)
return TypedDictType(
items, required_keys, readonly_keys, is_closed, fallback, self.line, self.column
items,
required_keys,
readonly_keys,
fallback,
self.line,
self.column,
is_closed=is_closed,
)
def zip(self, right: TypedDictType) -> Iterable[tuple[str, Type, Type]]:
Expand Down
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.