AttributeError: 'method' object has no attribute '__blueprintname__'
See original GitHub issueDescribe the bug
I am trying to structure my code following best practices, keeping everything contained within a class to avoid any global objects. When I attempt to contain my blueprints within the class object, I get the error in the title.
Code snippet
Minimal reproduction of the issue:
from sanic import Blueprint, Sanic
class Router(Sanic):
def __init__(self, *args, **kwargs):
super().__init__(*args, name="Example", **kwargs)
self._add_routes()
def _add_routes(self):
v1 = Blueprint("v1", version=1)
v1.add_route(self._handle_v1_foo, "/foo")
self.blueprint(v1)
v2 = Blueprint("v2", version=2)
v2.add_route(self._handle_v2_foo, "/foo")
self.blueprint(v2)
def _handle_v1_foo(self, request):
pass
def _handle_v2_foo(self, request):
pass
app = Router()
Expected behavior
No error.
Actual behavior
Traceback (most recent call last):
File "/private/tmp/tmp.ZKa5SvCjpA/main.py", line 26, in <module>
app = Router()
File "/private/tmp/tmp.ZKa5SvCjpA/main.py", line 8, in __init__
self._add_routes()
File "/private/tmp/tmp.ZKa5SvCjpA/main.py", line 13, in _add_routes
self.blueprint(v1)
File "/tmp/virtualenv/lib/python3.10/site-packages/sanic/app.py", line 527, in blueprint
blueprint.register(self, options)
File "/tmp/virtualenv/lib/python3.10/site-packages/sanic/blueprints.py", line 309, in register
future.handler.__blueprintname__ = self.name
AttributeError: 'method' object has no attribute '__blueprintname__'
Issue Analytics
- State:
- Created a year ago
- Comments:10 (6 by maintainers)
Top Results From Across the Web
python - Tensorflow Attribute Error: 'method' object has no ...
The attribute error here is raised because you can't set any attribute on a method object i.e; class Foo: def bar(self): print("bar") if ......
Read more >AttributeError: 'function' object has no attribute 'name' #1327
when i register a blueprint in my flask app,an error occurred,the error msg is : ` /usr/bin/python3.4.2 ...
Read more >'method' object has no attribute '_from_serialized' Error when ...
I have a class that compiles a model and im using custom metrics. Now, its not compiling. I'm getting AttributeError: 'method' object has...
Read more >Problem with training VAE : r/tensorflow - Reddit
Been trying recreate VAE from tutorial ( author does not respond for ... AttributeError: 'method' object has no attribute '_from_serialized'.
Read more >2. Blueprint Objects — Flask API - GitHub Pages
Represents a blueprint. A blueprint is an object that records functions that will be called with the BlueprintSetupState later to register functions or...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
As for the question… I would not call that a bug. You are using Sanic and Blueprints in a way that was not anticipated. Unfortunately instance methods (TMK) cannot have properties assigned to them the way that functions can. This is why you see that message. Albeit it is somewhat of a “hack” to assign properties to a function, it is part of the under-the-hood magic that enables proper naming of handlers. So it might be possible to allow for instance methods to be used like this, but it would require some changes.
Actually… TBH, the more I look at it, I think this is deprecated code anyway. We probably can remove that assignment completely since it is no longer in use or needed.