On Wed, Oct 20, 2021 at 9:18 AM Piotr Waszkiewicz <waszk...@gmail.com> wrote:
> Do you think about something along those lines? > ``` > phone = book.publisher.owner.phone except AttributeError: None > ``` > Yes, that seems reasonable. > I don't mind this syntax but it would have to be supported by static type > checkers and IDEs. And currently something like this is not: > ``` > try: > phone = book.publisher.owner.phone > except AttributeError: > phone = None > ``` > > mypy complains: > ``` > error: Item "None" of "Optional[Publisher]" has no attribute "owner" > ``` > That sounds like a feature request for mypy. Would creating a new operator make it easier to implement analysis of that situation would mypy? My guess is not. Checking the AST to see if there's a try/except AttributeError sounds comparable to checking for the use of a none-aware operator. I'm completely ignorant of how mypy does its analysis, so that's just a wild guess. If PEP 505 is accepted, it would need support in the `operator` module. >> Might as well design that aspect of the implementation now. >> > > I'm sorry but I don't know if I understand that sentence correctly. You > mean we would have to add an "explicit" function that behaves like a > maybe-dot operator? Is it actually a requirement when adding new operators? > The documentation of the `operator` module says, "The operator module exports a set of efficient functions corresponding to the intrinsic operators of Python." It feels like there's an implicit "all" in there. The table of correspondences looks exhaustive. I haven't noticed any exceptions. https://docs.python.org/3/library/operator.html#mapping-operators-to-functions
_______________________________________________ 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/46H5MYVVMQMWS4DVJYVFGKWXS3CNYTJC/ Code of Conduct: http://python.org/psf/codeofconduct/