Earlier quoted context omitted.
Don't do this stupid party trick with `global`. You may as well argue that unit tests are pointless because you could cheat by making the implementations return just the hardcoded values from the test cases.
This isn't a "party trick" with `global`, it's a fundamental hole in the type system: class C: def __init__(self) -> None: self.i : int | list[int] = 0 def foo(self) -> None: self.i = [] def bar(self) -> int: if isinstance(self.i, int): self.foo() return self.i return 0 print(type(C().bar()))
class C {
i: number | number[];
constructor() {
this.i = 0;
}
foo(): void {
this.i = [];
}
bar(): number {
if (typeof this.i === 'number') {
this.foo();
return this.i;
}
return 0;
}
}
console.log(typeof new C().bar());
It seems to be a problem with "naked" type unions in general. It's unfortunate.