Search code examples
pythonbrython

Python/Brython TypeError: Cannot use cached_property instance without calling __set_name__ on it


I'm a network engineer, new to programming, so if I leave out any details do let me know. Trying my best to explain the problem here :) Getting a solution to this problem is very important for me, so any inputs would be highly appreciated.

Problem Statement: I wrote a python script, which is used by a brython script (used in html like javascript, converted to javascript at compile time). Basically when I click on a button on my webpage it triggers a python script which I have mentioned in my html like this:

*<body onload="brython()">
<script type="text/python" src="ip_tools.py"></script>*

The python script looks like this:

from browser import document as doc, bind
import ipaddress
def subcalc(ev):
    #Using the ipaddress module I get the ip_network, so the value ip will be like 192.168.1.0/24 (an object of ipaddress class)
    ip = ipaddress.ip_network(doc['ipadd'].value + '/' + doc['ipv4_mask'].value, strict=False)

    #This line gives the aforementioned error in Chrome Console
    *print(ip.hostmask)*

doc["sub_cal"].bind("click", subcalc)  #This line triggers the subcalc function when user clicks on a button with id-"sub_cal" on the webpage.

The complete error from Chrome Console:

error in get.apply Error
    at _b_.TypeError.$factory (eval at $make_exc (brython.js:7647), <anonymous>:161:327)
    at __get__491 (eval at exec_module (brython.js:8991), <anonymous>:5898:62)
    at __BRYTHON__.builtins.object.object.__getattribute__ (brython.js:5332)
    at Object.$B.$getattr (brython.js:6731)
    at subcalc0 (eval at $B.loop (brython.js:5230), <anonymous>:120:48)
    at HTMLButtonElement.<anonymous> (brython.js:12784) brython.js:5334 get attr hostmask of Object brython.js:5335 function () { [native code] } brython.js:6108 Traceback (most recent call last): TypeError: Cannot use cached_property instance without calling __set_name__ on it.

I have similar script working fine, the only difference is that this script uses an imported Python library other scripts do not use such libraries, except the browser library (which is required by Brython to work with html content)


Solution

  • Thanks for reporting this, it was a bug in the implementation of PEP 487 in Brython. It is fixed in this commit.