On 4/22/22 23:41, Mehdi2277 wrote:
My main question for this approach is how would this work with type checkers?
It would be new syntax for Python, so type checkers would have to
understand it.
Is there any restriction that forward class's continuation must appear in same
module?
No.
If it's allowed that a forward class may be continued in a different module I
do not see how type checker like mypy/pyright could handle that. Classes are
generally viewed as closed and fully defined within type checker. Monkey
patching at runtime later is not supported.
If it became official Python syntax, I suspect they'd figure out a way
to support it.
They might require that the expression used in the "continue class"
statement map to the original "forward class" declaration, e.g. they
might stipulate that they don't support this:
forward class X
random_name = X
continue class random_name:
...
But rather than speculate further, perhaps someone who works on one of
the static type analysis checkers will join the discussion and render an
informed opinion about how easy or hard it would be to support "forward
class" and "continue class".
One other edge case here is how would you forward declare an annotation for a
type that like this,
if TYPE_CHECKING:
import numpy
def f(x: numpy.ndarray) -> None:
...
forward declaring ndarray here would not make numpy.ndarray available.
In this case, adding forward declarations for classes in "numpy" would
be up to the "numpy" module. One approach might look more like this:
import numpy # contains forward declarations
if TYPE_CHECKING:
import numpy.impl
Though numpy presumably couldn't do this while they still supported
older versions of Python. That's one downside of using new syntax--you
can't use it until you stop support for old versions of Python that
predate it.
Would you forward declare modules? Is that allowed?
I haven't proposed any syntax for forward-declaring modules, only classes.
I'm confused in general how if TYPE_CHECKING issue is handled by this approach.
Usually class being imported in those blocks is defined normally (without
continue class) somewhere else.
My proposal should mate well with "if TYPE_CHECKING". You would define
your forward classes in a module that does get imported, but leave the
continue classes in a separate module that is only imported "if
TYPE_CHECKING", as per my example with "numpy" above.
//arry/
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at
https://mail.python.org/archives/list/python-dev@python.org/message/A7MW3B7JYM2LT4VUCAE7OUHJSYEX76G7/
Code of Conduct: http://python.org/psf/codeofconduct/