Coding 2022-04-09
Okay, so, I've been tweaking MOTR's typing plugin a bit, and I think I know generally how to make some improvements, but not yet how to handle the exact details. I'll work on the other tests tomorrow, hopefully.
For now, let's get some plans together for the plugin.
First off, here's what I'm trying to solve:
- The get() method defines two overloads. One with no default, and one with a default.
- The existing plugin code, which focuses on rewriting the signature, can handle the first case, but not the second.
- This manifests as a Union[V, T] getting treated everywhere as V, which is unhelpfully restrictive.
Various ways of reworking the existing signature rewrite code have done nothing to change this, but at least they didn't break the other cases.
I've come to the conclusion that I need to make the following changes:
- At signature rewrite time, convert the default and return types to Any. I could just change the code, but I think leaving the code as-is will be easier to understand. At minimum, the default type needs to be changed.
- Add a method rewrite hook that, when the default argument is present, constructs a union type manually to be the return type.
Let's see what happens when I try...
...
All right, I've generated an Any, now let's see about the return value.
I found a function called make_simplified_union that seems to do exactly what I want, so it looks like I'm in business. Some of mypy's internals kind of confuse me, so I'm probably doing something wrong, but I've gotten the tests to pass.
Speaking of tests, I really should get to work on the flake8 wrapper tests tomorrow. For now though, I've spaced out enough while finishing this.
Good night.