Skip to content

Commit

Permalink
Ignore typing errors for constructors
Browse files Browse the repository at this point in the history
Firstly, it's worth noting that this only ignores the typing error.
Things that call these APIs will still be checked! The problem is that
the checking may be incorrect because of the API itself.

There's a long discussion about this and related Liskov issues at:
    python/mypy#1237

The short version is that this API is wrong-ish, but there's no
mechanism within mypy to correctly annotate what's going on here. There
*is* some of this around, because mypy treats __init__ and __new__
differently itself, but we can't apply that treatment to our
constructors.

This would be better if we actually knew which methods were
constructors, instead of the name-based guessing here... but that's way
too much complexity for me right now.
  • Loading branch information
kaiw committed Dec 25, 2018
1 parent c1896b1 commit 656afda
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions pgidocgen/stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ def __init__(self, classname):
def add_member(self, member):
self.members.append(member)

def add_function(self, function):
def add_function(self, function, *, ignore_type_error=False):
if ignore_type_error:
function += " # type: ignore"
self.functions.append(function)

@property
Expand Down Expand Up @@ -283,7 +285,17 @@ def stub_class(cls) -> str:
stub.add_member(format_field(f))

for v in cls.methods + cls.vfuncs:
stub.add_function(stub_function(v))
# GObject-based constructors often violate Liskov substitution,
# leading to typing errors such as:
# Signature of "new" incompatible with supertype "Object"
# While we're waiting for a more general solution (see
# https:/python/mypy/issues/1237) we'll just ignore
# the typing errors.

# TODO: Extract constructor information from GIR and add it to
# docobj.Function to use here.
ignore = v.name == "new"
stub.add_function(stub_function(v), ignore_type_error=ignore)

return str(stub)

Expand Down

0 comments on commit 656afda

Please sign in to comment.