adding __name__
attribute to a callable Protocol
doesn't work with function objects and breaks inference with Protocol
objects
#12976
Labels
Bug Report
For context, I'm lightly abusing Mypy's model of the world to pass
Protocol
objects around at runtime; hopefully by the time things are fixed so thatProtocol
s no longer look instantiatable we'll also have #9773 or something to allow for more runtime dynamism using them ;)However, the bug in question actually shows up with function objects as well, albeit with a slightly different way.
Consider the following program:
I get the following output:
If I comment out the
__name__
attribute ofNamedCallable
, the errors go away and I instead get this:Given that the revealed type is
def () -> SomeProtocol
for both the protocol object itself and the function, I am surprised thatg
silently degrades toAny
butg2
requests an explicit annotation. It is also odd that the__name__
attribute, which is present on both of these objects at runtime, seems to be an impediment here.So there area a few bugs here:
actual_function
andSomeProtocol
are in fact different objects internally to Mypy (one thinking it has a__name__
and the other doesn't) perhaps the string they show inreveal_type
ought to be visibly distinct?__name__
attributes and it seems like they ought to be inferredAny
here with no warning in theProtocol
case?Protocol
s aren't actually functions that return themselves (but please don't fix this one yet unless we have some other way to pass these around 😉).Your Environment
mypy.ini
(and other config files): noneThe text was updated successfully, but these errors were encountered: