Skip to content

Commit 656afda

Browse files
committed
Ignore typing errors for constructors
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.
1 parent c1896b1 commit 656afda

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

pgidocgen/stubs.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ def __init__(self, classname):
7171
def add_member(self, member):
7272
self.members.append(member)
7373

74-
def add_function(self, function):
74+
def add_function(self, function, *, ignore_type_error=False):
75+
if ignore_type_error:
76+
function += " # type: ignore"
7577
self.functions.append(function)
7678

7779
@property
@@ -283,7 +285,17 @@ def stub_class(cls) -> str:
283285
stub.add_member(format_field(f))
284286

285287
for v in cls.methods + cls.vfuncs:
286-
stub.add_function(stub_function(v))
288+
# GObject-based constructors often violate Liskov substitution,
289+
# leading to typing errors such as:
290+
# Signature of "new" incompatible with supertype "Object"
291+
# While we're waiting for a more general solution (see
292+
# https://github.com/python/mypy/issues/1237) we'll just ignore
293+
# the typing errors.
294+
295+
# TODO: Extract constructor information from GIR and add it to
296+
# docobj.Function to use here.
297+
ignore = v.name == "new"
298+
stub.add_function(stub_function(v), ignore_type_error=ignore)
287299

288300
return str(stub)
289301

0 commit comments

Comments
 (0)