brython-dev / brython

Brython (Browser Python) is an implementation of Python 3 running in the browser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

When extending JS classes, `JavascriptError: TypeError: bases is undefined` error

denis-migdal opened this issue · comments

from browser import window

class X( window.HTMLElement ):
    def __init__(self):
        print("ok")
    def foo(self):
        pass
    
print(dir(X))
y = X.__new__(X)

y.__init__()

Produces

['__bases__', '__class__', '__dict__', '__module__', '__mro__', '__name__', '__qualname__']
Traceback (most recent call last):
  File <string>, line 10, in <module>
JavascriptError: TypeError: bases is undefined

When I should expect:

['__bases__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__flags__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__mro__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__subclasshook__', 'foo']
ok

Brython does its best to interact with Javascript, but here the Python class X is supposed to inherit window.HTMLElement, which is not a Javascript (equivalent of a) class, but an interface : new HTMLElement() fails with an Illegal constructor error message.

In the commit above, an exception is raised when a subclass of window.Node is used as a base class. Maybe not optimal, but at least it avoids the previous useless error message "bases is undefined".

I do not think the issue comes from HTMLElement specifically, as

from browser import window



class X( window.Map ):
    def __init__(self):
        print("ok")
    def foo(self):
        pass
    
print(dir(X))
y = X.__new__(X)

y.__init__()

Produces the same error.