-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Discourage inheritance from some aiohttp classes #2691
Comments
Why would you prefer aggregation over inheritance in the special case of It seems to be a viable design decision, when creating simple applications. It helps to structure it. |
It seems not.
|
How would you access the aggregating object from |
You can push the reference to your aggregating instance into the application as |
I guessed that you will propose that solution, but of course I wanna argue. :) So looking at it we have two objects referencing each other, just because inheritance is discouraged:
Which is a viable solution of course, but don't you find it to be a not-so-good OOP design? Not to say, it breaks static type checking because tools don't know what is |
Software design is always a compromise.
I understand that the solution is not ideal. Suppose you have something like
Eventually, in aiohttp we decided to add a Replacing string keys for application storage with a ContextVar-styled object is an interesting idea. |
@asvetlov class AsyncTestSession(aiohttp.ClientSession):
"""
Rewrites request netloc (host:port) and prepends base_path.
"""
def __init__(self, netloc, base_path='', *args, **kwargs):
super().__init__(*args, **kwargs)
self.netloc = netloc
self.base_path = base_path
def _request(self, method, url, *args, **kwargs):
scheme, netloc, path, params, query, fragment = urllib.parse.urlparse(url)
if path:
path = '/'.join((self.base_path, path.lstrip('/')))
url = urllib.parse.urlunparse((scheme, self.netloc, path, params, query, fragment))
return super()._request(method, url, *args, **kwargs) We store |
In first line I see
web.Application
andClientSession
.If user want to extend functionality -- I'd like to encourage aggregation, not inheritance.
It could be done easy by adding
__subclass_init__
thunder method, the method should generate a warning (UserWarning
maybe).The method is not called by Python 3.5 (but not generates a error too), on Python 3.6+ everything should work fine. Who cares about Python 3.5?
The text was updated successfully, but these errors were encountered: