[pypy-dev] Getting rid of "prebuilt instance X has no attribute Y" warnings

Armin Rigo arigo at tunes.org
Fri Dec 19 00:16:18 CET 2014


Hi Timothy,

On 9 December 2014 at 06:01, Timothy Baldridge <tbaldridge at gmail.com> wrote:
> I'm getting a ton of these sort of warnings. They seem to go away when I
> either a) type hint the object via assert (gross) or b) access the attribute
> via a getter method. Is there a better way? Would there be a problem with
> somehow just turning this warning off?

The problem is that attributes get moved unexpectedly to base classes.
Then all instances of any subclass of that base class will have all
the attributes, i.e. be unexpectedly fat.  This is why e.g. in PyPy we
added this to a few crucial base classes:

    class W_Root(object):
        _attrs_ = ()       # can also be __slots__ = ()

It crashes the annotator when attributes (other than those listed,
which is none in that case) are unexpectedly moved to that base class.
You're seeing the problem as warnings about prebuilt instances'
attributes, but the problem more generally applies to all instances,
prebuilt or not.

How to fix that depends on your code.  Usually you need to figure out
a bit more context, i.e. seeing where the read or write of the
attribute occurs and trying to understand why the annotator thinks the
object in question can be of any subclass of the root class rather
than only some specific subclass.  Sometimes you really need "assert
isinstance(x, Subclass)" but usually the problem can be fixed more
cleanly.


A bientôt,

Armin.


More information about the pypy-dev mailing list