<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Sat, Dec 20, 2014 at 2:00 PM, Dennis Brakhane <span dir="ltr"><<a href="mailto:brakhane@googlemail.com" target="_blank">brakhane@googlemail.com</a>></span> wrote:<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Maybe I'm missing something, but wouldn't type hinting as it's defined<br>
now break "virtual subclassing" of ABC?<br>
<br>
For example, given the following code:<br>
<br>
  from collections import Sequence<br>
<br>
  class MySequence:<br>
    ...<br>
<br>
  Sequence.register(MySequence)<br>
<br>
it seems to me like the following would work:<br>
<br>
  def foo(bar):<br>
      if not isinstance(bar, Sequence):<br>
          raise RuntimeError("Foo can only work with sequences")<br>
      ...<br>
<br>
but when rewritten for static type checking<br>
<br>
    def foo(bar: Sequence):<br>
        ....<br>
<br>
it would cease to work. At least I don't see a way a static type checker<br>
could handle this relaiably (the register call might occur anywhere,<br>
after all)<br>
<br>
Is this intentional?<br>
<br>
Even if this might be more a mypy/implementation question, it should be<br>
clear to users of typing.py if they should expect ABCs to break or not<br clear="all"></blockquote><div><br></div><div>Well, the program will still run fine with CPython, assuming you keep the isinstance() check in your program. The argument declaration does not cause calls with non-Sequence arguments to be rejected, it just guides the static checker (which is a program that you must run separately, in a similar way as a linter -- it is not built into CPython).<br><br></div><div>The static checker may request the call foo(MySequence()) if it cannot see that MySequence is a Sequence. You have two options there: if you can change the code of MySequence, you should just inherit from Sequence rather than using the register() call; if you cannot change the code of MySequence, you should use a *stub* module which declares that MySequence is a Sequence. You can read up on stub modules in the mypy docs: <a href="http://mypy.readthedocs.org/en/latest/basics.html#library-stubs">http://mypy.readthedocs.org/en/latest/basics.html#library-stubs</a><br></div></div><br>-- <br><div class="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido">python.org/~guido</a>)</div>
</div></div>