mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Cannot reassign `typing.final`

Open A5rocks opened this issue 2 years ago • 2 comments

Bug Report

When I assign typing.final to another name, it stops decorating things as final.

The usecase for this is to add runtime behavior to my final classes by making the actually exported final something else under an if not TYPE_CHECKING, though I can also imagine that backports (re-exporting either typing_extensions.final or typing.final) run into this issue.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.11&gist=85c39c943895a1eaa6d1c88b55001e8a

Expected Behavior

Errors in both cases.

Actual Behavior

Error in only the first case.

Your Environment

  • Mypy version used: 1.4.0
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.11

A5rocks avatar Jun 27 '23 00:06 A5rocks

Yeah, it's pretty common for mypy to not track the special effects of symbols when reassigned. A general trick that works in these cases is from typing import final as cooler_final

hauntsaninja avatar Jun 27 '23 03:06 hauntsaninja

Oh yeah I forgot that trick exists. (I'm aware of why this is generally restricted, cause we don't have all the information at the pass time, but I don't really know when exactly typeinfo creation happens or if we can edit typeinfos afterwards)

A5rocks avatar Jun 27 '23 05:06 A5rocks