python / python/mypy

Spurious redef warning in an odd situation with type(None) plus TypeAlias plus a later # type: ignore

Open
#17,593 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Really bizarre behavior here that I can't figure out. Probably not high priority as it's likely an extreme edge case.

This code:

from typing import TypeAlias

abc: TypeAlias = type(None)

x = y # type: ignore

generates the following error in mypy 1.10.1 running on Python 3.11.8:

tt.py:3: error: Name "abc" already defined on line 3  [no-redef]
Found 1 error in 1 file (checked 1 source file)

This is obviously wrong, as abc is not a redefinition (and you can rename it to whatever you want, you get the same error).

This error does NOT happen if you do any of the following:

  1. Use type instead of TypeAlias as the declared type of abc.
  2. Use any value other than type(None) as the value of the expression (if you use type(5), for example, you get the slightly more reasonable although still inaccurate error message that the expression isn't a valid type; I think it's reasonable to expect type literals only rather than at-runtime types though, so this is understandable.
  3. If you don't include the last line which generates a second mypy error (in that case, you get NO error on the abc line, not even the "not a valid type" error you'd get for assigning to type(5)).

The presence of the # type: ignore comment is optional as it turns out, it doesn't matter whether the second error is generated or suppressed, just that there is a second error in the file.

What should happen? IDK fix the underlying bug here. Probably we should get a "not a valid type" error similar to the type(5) situation, and of course that should happen regardless of whether there are or aren't errors elsewhere in the file.

Workaround: import NoneType from the types module is probably cleanest.

I'll mention here for others who might find this looking for how to use NoneType as a type annotation that you'll get a warning from mypy if you use NoneType as a type hint, saying you should use None instead (and yo should probably do that). The code that inspired this used NoneType not as a type hint but for a runtime isinstance check.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with the minimal Python snippet in the report under mypy 1.10.1, then compare behavior with and without TypeAlias, type(None), and the later ignored assignment. Trace the handling of the alias and the later error-producing line; done means no spurious no-redef error and a consistent invalid-type diagnostic regardless of other errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.