Discussion: Duck typing with “concepts”
So here’s an interesting idea, not a proposal yet. In C++20, a Concept is a list of Boolean expressions with a name that can be used in place of a type in a templated (ie type-generic) function. from typing import Concept Iterator = Concept(lambda o: hasattr(o, "__iter__", lambda o: iter(o) != NotImplemented) # Concept inheritance Iterable = Concept(lambda o: hasattr(o, "__next__"), Iterator) You could use concepts to define many practical “real-world” duck types. A concept is like an opt-in duck typing type assertion. Since it’s a part of type annotation syntax, assertions can be generated automatically by looking at assertions. You would use a Concept like any other type in type annotations. Marko, how do you think Concepts might integrate with icontract? (I’m imagining overriding an import hook to automatically add contracts to functions with concepts.) How frequently do you use duck typing at Parquery? How might Concepts affect how often you used duck typing?
I think you may be a bit late. Have you heard about PEP 544? -- Ivan On Tue, 22 Jan 2019 at 11:50, James Lu <jamtlu@gmail.com> wrote:
So here’s an interesting idea, not a proposal yet.
In C++20, a Concept is a list of Boolean expressions with a name that can be used in place of a type in a templated (ie type-generic) function.
from typing import Concept Iterator = Concept(lambda o: hasattr(o, "__iter__", lambda o: iter(o) != NotImplemented) # Concept inheritance Iterable = Concept(lambda o: hasattr(o, "__next__"), Iterator)
You could use concepts to define many practical “real-world” duck types.
A concept is like an opt-in duck typing type assertion. Since it’s a part of type annotation syntax, assertions can be generated automatically by looking at assertions.
You would use a Concept like any other type in type annotations.
Marko, how do you think Concepts might integrate with icontract? (I’m imagining overriding an import hook to automatically add contracts to functions with concepts.) How frequently do you use duck typing at Parquery? How might Concepts affect how often you used duck typing? _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Hi James, As Ivan has mentioned, Protocols already allow for statical type checks: https://mypy.readthedocs.io/en/latest/protocols.html We didn't need protocols that often at Parquery, maybe half a dozen of times? While we didn't use them in Python, we had to use them intensively in Go where it is a bit of a nightmare. It gives you freedom for cases when your input arguments are fairly general (e.g., in a public library), but it made refactoring our _production_ code (i.e. specific in contrast to general) much harder since you couldn't look up easily which type implements which "interface" (as protocols are called in Go). Cheers Marko
participants (3)
-
Ivan Levkivskyi
-
James Lu
-
Marko Ristin-Kaufmann