Hello all,

I am working on an expression and replacement system in Python and am trying to 
figure out if constructors for generic classes should be done with classmethods 
or with functions. I have laid out an example using each in a Github issue 
(https://github.com/Quansight-Labs/metadsl/issues/69#issuecomment-500898932) 
and wanted to open the issue up the larger community because I am unsure if 
there is an established pattern here yet.

A minimal example showing the difference is `none(int)` vs 
`Optional[int].none()`. (Note Optional here is not typing.Optional, but my own 
class).

This has implications for the language, because if we want to support 
Optional[int].none() then we need to be able to access the generic type 
arguments at runtime which is currently impossible (without monkeypatching some 
typing internals) see https://github.com/python/typing/issues/629. 

But if we think the functional approach is more appropriate then we should be 
able to pass in TypeVars to functions at runtime, so we can define replacements 
on this form (see the linked issue above for the example). This would require 
fixing https://github.com/python/mypy/issues/6964.

I would appreciate any thoughts on which is more Pythonic and more likely to 
continue being relevant as the typing conversation evolves. 

The underlying motivation is that Python is increasingly being used to build up 
computation that executes on other runtimes (TensorFlow, PyTorch, even if they 
try to hide it now) and type annotations give a nice UX for describing these 
APIs. We need to be able to inspect them at runtime to build up the right 
computation graphs, but we also want them to valid under static systems like 
MyPy, to continue using all the benefits it provides for dev productivity.
_______________________________________________
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/75MF2B62OGVX2BVN6N6ZZE4ZRWO4JA6F/

Reply via email to