Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes how ** affects inference in {} expressions, refs #11691 #11693

Closed
wants to merge 6 commits into from
Closed

Fixes how ** affects inference in {} expressions, refs #11691 #11693

wants to merge 6 commits into from

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Dec 9, 2021

This is a quite common problem, I've seen multiple issue reports about this.

So, the problem is: when a person uses {**kwargs}, it uses the same logic we have for .update() call. But, this is not right. .update() indeed needs matching types.
But, unpacking into a new dict should be allowed with any values.

To achive that I've decided to use join for key and value types. This is how it works:
小薪懈屑芯泻 褝泻褉邪薪邪 2021-12-09 胁 18 38 15

You may ask: why not Union? dict[Union[type, float], str] looks strange to me. Also, some other similar places use join. But, I am open to discuss this.

I will add extra tests a bit later, I am currently too tired 馃槥

Closes #11691
Refs #11151

@@ -3334,13 +3334,14 @@ def check_lst_expr(self, items: List[Expression], fullname: str,
self.chk.named_generic_type(fullname, [tv]),
self.named_type('builtins.function'),
name=tag,
variables=[tv])
out = self.check_call(constructor,
Copy link
Member Author

Choose a reason for hiding this comment

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

This is a readability change only. Before and after:
小薪懈屑芯泻 褝泻褉邪薪邪 2021-12-09 胁 18 41 40
小薪懈屑芯泻 褝泻褉邪薪邪 2021-12-09 胁 18 41 52

@github-actions

This comment has been minimized.

mypy/join.py Outdated
key, value = extract_key_value_types(item, mapping_type)
keys.append(key)
values.append(value)
return join_type_list(keys), join_type_list(values)
Copy link
Member Author

Choose a reason for hiding this comment

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

I think that we also need to handle: NoReturn and TypedDict types here. Any others?

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

mypy/checkexpr.py Outdated Show resolved Hide resolved
mypy/checkexpr.py Outdated Show resolved Hide resolved
@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

graphql-core (https://github.com/graphql-python/graphql-core)
+ src/graphql/utilities/extend_schema.py:200: error: Unused "type: ignore" comment
+ src/graphql/utilities/extend_schema.py:245: error: Unused "type: ignore" comment
+ src/graphql/utilities/extend_schema.py:353: error: Unused "type: ignore" comment
+ src/graphql/utilities/extend_schema.py:362: error: Unused "type: ignore" comment

pandera (https://github.com/pandera-dev/pandera)
+ pandera/schemas.py:324: error: Incompatible return value type (got "Dict[Any, object]", expected "Dict[str, str]")  [return-value]

rclip (https://github.com/yurijmikhalevich/rclip)
- rclip/db.py:70: error: Argument 1 to "update" of "dict" has incompatible type "NewImage"; expected "Mapping[Any, None]"

ibis (https://github.com/ibis-project/ibis)
- ibis/backends/base/sql/registry/main.py:361: error: Argument 1 to "update" of "dict" has incompatible type "Dict[AnnotableMeta, Any]"; expected "Mapping[Type[Node], Any]"
- ibis/backends/impala/compiler.py:25: error: Argument 1 to "update" of "dict" has incompatible type "Dict[AnnotableMeta, Any]"; expected "Mapping[Type[Node], Any]"
+ ibis/backends/clickhouse/compiler.py:69: error: Incompatible types in assignment (expression has type "Dict[Type[Node], Any]", base class "ExprTranslator" defined the type as "Dict[AnnotableMeta, Any]")

black (https://github.com/psf/black)
+ src/blib2to3/pgen2/tokenize.py:581:31: error: Incompatible types in assignment (expression has type "Optional[Pattern[str]]", variable has type "Pattern[str]")
+ src/blib2to3/pgen2/tokenize.py:603:29: error: Incompatible types in assignment (expression has type "Optional[Pattern[str]]", variable has type "Pattern[str]")

core (https://github.com/home-assistant/core)
+ homeassistant/components/sentry/__init__.py:207: error: Unused "type: ignore" comment

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

spread dict not used to infer type
3 participants