Skip to content

Fix isinstance on namedtuple type aliases#21103

Open
paulorochaoliveira wants to merge 2 commits intopython:masterfrom
paulorochaoliveira:bug/namedtuple-typealias-isinstance
Open

Fix isinstance on namedtuple type aliases#21103
paulorochaoliveira wants to merge 2 commits intopython:masterfrom
paulorochaoliveira:bug/namedtuple-typealias-isinstance

Conversation

@paulorochaoliveira
Copy link
Copy Markdown

Summary

Fix #21100
Fix a false positive where isinstance(x, Alias) reports
Parameterized generics cannot be used with class or instance checks
when Alias is a TypeAlias to a class inheriting from NamedTuple.

Changes

  • Exempt namedtuple-backed tuple aliases from the generic-alias isinstance rejection path
  • Add a regression test for TypeAlias = Foo where Foo is a NamedTuple

Testing

  • ./venv/bin/pytest -n0 mypy/test/testcheck.py -k "testIsinstanceTypeAliasToNamedTuple or testIsinstanceTypeArgsAliases or testAliasNonGeneric"
  • ./venv/bin/pytest -n0 mypy/test/testcheck.py -k check-isinstance.test

@github-actions

This comment has been minimized.

@github-actions
Copy link
Copy Markdown
Contributor

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

@@ -557,6 +557,10 @@ def visit_call_expr_inner(self, e: CallExpr, allow_none_return: bool = False) ->
and node
and isinstance(node.node, TypeAlias)
and not node.node.no_args
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I feel like no_args should be True in this case? In which case this if statement is fine.

I'd rather that, unless there's a good reason for existing behavior...

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.

isinstance doesn't work with TypeAlias for classes inherited from NamedTuple

3 participants