Skip to content

Conversation

sterliakov
Copy link
Collaborator

@sterliakov sterliakov commented Sep 1, 2025

Fixes #19716. It is a follow-up to #19767 and was missed there due to malformed test stubs (the same testcase fails when run by full mypy against typeshed, I did not notice missing AwaitExpr because of the passing test...). I synced typevar variance with typeshed definitions and added AwaitExpr to the list of context-dependent exprs. Cc @ilevkivskyi

@sterliakov
Copy link
Collaborator Author

sterliakov commented Sep 1, 2025

(and confirmed that if False now makes that test fail as expected)

I'm surprised that this was the only problem caused by stub definitions. I'll open a follow-up PR to sync variance in other typing-* fixtures, this is just begging for trouble, all of them are using variances incompatible with typeshed definitions.

@sterliakov sterliakov marked this pull request as ready for review September 1, 2025 18:57

This comment has been minimized.

Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

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

Thanks for the catch! I have one suggestion.

if isinstance(s.expr, (CallExpr, ListExpr, TupleExpr, DictExpr, SetExpr, OpExpr)):
if isinstance(
s.expr, (CallExpr, ListExpr, TupleExpr, DictExpr, SetExpr, OpExpr, AwaitExpr)
):
Copy link
Member

Choose a reason for hiding this comment

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

What about using isinstance(s.expr, AwaitExpr) and isinstance(s.expr.expr, CallExpr)? This will save us some pointless busy-work for cases like return await some_name.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hm, yes, makes sense. I doubt that await not_a_call accounts for some significant portion of await expressions, but this shouldn't harm.

Copy link
Contributor

github-actions bot commented Sep 2, 2025

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@ilevkivskyi ilevkivskyi merged commit 2ae0667 into python:master Sep 2, 2025
20 checks passed
hauntsaninja pushed a commit that referenced this pull request Sep 17, 2025
Originally discovered in #19777. Our test fixtures use definitions that
are very far from their real counterparts, but at least generics should
match if possible. Let's see if it kills more than one testcase
(expected from #19777).
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.

Spurious type error with async function + generic callable argument + optional return type
2 participants