[Python-Dev] Return type of alternative constructors

Nick Coghlan ncoghlan at gmail.com
Tue May 10 09:21:57 EDT 2016


On 10 May 2016 at 02:30, Guido van Rossum <guido at python.org> wrote:
> On Sun, May 8, 2016 at 7:52 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:
>> P.S. It occurs to me that a sufficiently sophisticated typechecker
>> might be able to look at all of the calls to "cls(*args, **kwds)" in
>> class methods and "type(self)(*args, **kwds)" in instance methods, and
>> use those to define a set of type constraints for the expected
>> constructor signatures in subclassses, even if the current code base
>> never actually invokes those code paths.
>
> Could you restate that as a concrete code example? (Examples of the problems
> with "construction features" would also be helpful, probably -- abstract
> descriptions of problems often lead me astray.)

Rectangle/Square is a classic example of the constructor signature
changing, so I'll try to use that to illustrate the point with a
"displaced_copy" alternate constructor:

    class Rectangle:
        def __init__(self, top_left_point, width, height):
            self.top_left_point = top_left_point
            self.width = width
            self.height = height

        @classmethod
        def displaced_copy(cls, other_rectangle, offset):
            """Create a new instance from an existing one"""
            return cls(other.top_left_point + offset, other.width, other.height)

    class Square:
        def __init__(self, top_left_point, side_length):
            super().__init__(top_left_point, side_length, side_length)

At this point, a typechecker *could* have enough info to know that
"Square.displaced_copy(some_rectangle, offset)" is necessarily going
to fail, even if nothing in the application actually *calls*
Square.displaced_copy.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list