On Thu, Apr 22, 2021 at 10:47 AM Matthew Einhorn <matt@einhorn.dev> wrote:
> In PyCharm, the above code will result in it highlighting the number `12` with the following warning: "Type 'int' doesn't have expected attribute 'close'"

Which gives yet another use for type hints: helping out IDEs.


> If instead you add an `elif isinstance(t, str):`, under that condition it'll auto-complete `t.` with all string properties/methods.

now this makes me nervous -- if folks start adding isinstance checks to make their IDE more helpful , we are rally getting away from Duck Typing.

However, you could have presumably typed it as Sequence[int] and gotten all the benefits of duck typing and IDE completion.

However, if code were to really use a duck-typed "str-like" i would produce a failure in the type checker even if it was perfectly functional.

NOTE: str is not a great example, as it's one type that we often do need to explicitly check -- to make the distinction between a Sequence of strings, which a str is, and a str itself.  And str is so fundamental and complex a type it's rarely duck-typed anyway.

-CHB


_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@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/EGBSQALPGCTLAPM6FLIQLDV2YD2OLVAB/
Code of Conduct: http://python.org/psf/codeofconduct/


--
Christopher Barker, PhD (Chris)

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython