basedmypy icon indicating copy to clipboard operation
basedmypy copied to clipboard

make `allow-redefinition` better / require annotation

Open KotlinIsland opened this issue 3 years ago • 1 comments

a: int | str = 1
reveal_type(a)  # int | str
a = ""
reveal_type(a)  # str

This is actually redefining a as str, not reassigning as part of the union.

What if it always needed a type annotation to redefine:

a: int | str = 1
a = str(a)  # no redefine
a = 1  # no error, and no redefine 👍

should need:

a: int | str = 1
a: str = str(a)
a = 1 # error 👍

Using Infer would also be usable here no announce a redefinition

a = 1
a: Infer = str(a)

Perhaps even a separate annotation Redefine

KotlinIsland avatar Feb 22 '22 23:02 KotlinIsland

I think it best to require explicit redefinition AND supply the new type (even an explicit type annotation may be a mistake, such as trying to add a new variable to a long function, and there already is one with that name).

Something like:

a: int = 5
print(a)
a: Redefine[str] = "foo"

def foo(a: int)->None:
    a: Redefine[str] = str(a)
    ...

Zeckie avatar May 30 '22 07:05 Zeckie