-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Make AppKey compatible with string keys #7555
Conversation
a4a8d1f
to
f3331e0
Compare
Codecov Report
@@ Coverage Diff @@
## master #7555 +/- ##
=======================================
Coverage 97.35% 97.36%
=======================================
Files 106 106
Lines 31481 31488 +7
Branches 3575 3574 -1
=======================================
+ Hits 30648 30657 +9
+ Misses 630 629 -1
+ Partials 203 202 -1
Flags with carried forward coverage won't be shown. Click here to find out more.
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
Thanks for testing this out.
Could you show an example of what the problem is? I'd have thought that it would just be a find/replace job...
The problem this solves is that libraries with string keys have needed to use awkward naming conventions to reduce the likelihood (but not guarantee) that the keys won't clash with a user's own keys or those of another library. With AppKey, this is no longer a problem, but displaying the module name is needed to tell which key we're talking about. e.g. Your application might have a "db" key, aiohttp-admin might have a "db" key and aiohttp-session might have a "db" key. That's 3 different keys that could share the same name (currently, they use awkward names like "__aiohttp_session_db", but a library from another developer not aware of the issue could easily use a simpler string without realising that it's likely to clash with a portion of their user's own keys).
As above, that's rather the point, they are different keys. It would be more confusing to find your key now refers to aiohttp-admin's database connection and not the one you created in your application. |
Find and replace technically, however we don't control all parts. There are a lot of external component which we can't update directly / need to be updated by their developers after a release. Just starting to use # -- we control this --
class HomeAssistant: ...
KEY_HASS = AppKey[HomeAssistant]("hass")
hass = HomeAssistant()
app = web.Application()
-app["hass"] = hass
+app[KEY_HASS] = hass # -- external components --
# this would break without an update
hass = app["hass“] That's where the idea to make
Is this such a prevent issue? For -- app[KEY_HASS] = app["hass"] = hass |
It's not mangling the name, the object itself is the key. It's just the repr() that adds the module name in to help debugging.
Yeah, I feel like duplicating it on the string key makes the most sense for transitioning. |
The repr name is mangled with the the module name but you're right the key comparison is based on object id.
Not as beautiful as I would've liked but I the reasoning for keeping it the way it is makes sense. I'll go ahead and close this then. |
Sure, but really, this is just adding more information than we had before with string keys. If you print() the key object (e.g. when iterating through all keys in the Application), it should give you a very good idea of where that key was created and help you to find it a lot easier. |
What do these changes do?
Allow the simultaneous use of
AppKey
and string keys. During my early testing for Home Assistant I found it quite challenging to start usingAppKey
. At least in this particular case, a variable was stored once but read all throughout the code base. Just missing one update, would cause a test failure. By making both compatible, users will be able to update them gradually.The only downside being that the names of
AppKey
will no longer be able to contain the module name and loose some of their uniqueness. However, I don't believe that is an issue with the current string keys so it should be fine. It might even help avoid some additional confusions if AppKey instances have the same name + same variable name but are in different modules -> so not compatible.Overall, I found that without this change, I wasn't able to introduce
AppKey
during testing in a larger code base./CC @Dreamsorcerer
Are there changes in behavior for the user?
The feature hasn't been shipped yet.