Skip to content
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

refactor parse_url_overrides #3034

Merged
merged 7 commits into from
Jun 20, 2017
Merged

refactor parse_url_overrides #3034

merged 7 commits into from
Jun 20, 2017

Conversation

russellballestrini
Copy link
Contributor

Refactor parse_url_overrides:

  • pop values with default if key is missing
  • change conditionals to test for truth
  • prevent throwing an exception if passing keyword with default value
  • test if anchor starts with '#' before blindly adding it

Refactor parse_url_overrides:

* pop values with default if key is missing
* change conditionals to test for truth
* prevent throwing an exception if passing keyword with default value
* test if anchor starts with '#' before blindly adding it
	modified:   pyramid/url.py
Copy link
Member

@digitalresistor digitalresistor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good :D

pyramid/url.py Outdated

if '_port' in kw:
port = kw.pop('_port')
if anchor.startswith('#') == False:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

	modified:   pyramid/url.py
@russellballestrini
Copy link
Contributor Author

Hi @mmerickel and @bertjwregeer - what should I do next?

Copy link
Member

@mmerickel mmerickel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @russellballestrini, thanks for the reminder. This logic needs to be uniform between resource_url and route_url. Currently resource_url does this on its own but ideally it would be updated to use parse_url_overrides as well by munging the argument names or something.

Also annoyingly .pop is slower than if foo in bar: bar.get(foo) but I think it's not worth fretting about performance here.

@tseaver
Copy link
Member

tseaver commented May 9, 2017

Also annoyingly .pop is slower than if foo in bar: bar.get(foo) but I think it's not worth fretting about performance here.

dict.pop(key, default) is a mutating operation: it isn't equivalent to get(key, default), which should itself be faster than the if key in thing: foo = thing[key] variant.

@russellballestrini
Copy link
Contributor Author

russellballestrini commented May 9, 2017

The original version uses dict.pop(key) and intentionally mutates the kwargs. This actually surprised me and so I was considering using dict.get(key, default) when talking about it in IRC. Do we know why this original version uses pop? Could we (or should we) change it to get?

@tseaver
Copy link
Member

tseaver commented May 9, 2017

@russellballestrini The point of dict.pop is to consume the item, so that it is no longer present in the dict during later processing. This usage is particularly important when proxying to another function / method using **dict.

It is perfectly OK to use dict.pop(key, default) to avoid the if statement (similar to dict.get(key, default)).

@russellballestrini
Copy link
Contributor Author

russellballestrini commented May 9, 2017

@tseaver - Ah yes, I see how popping and mutating the kwargs could be useful, especially if we have multiple functions working on the same data with separate concerns.

@mmerickel - I see what you mean about resource_url having the same embedded logic - I'll take a stab at refactoring that too!

@mmerickel
Copy link
Member

mmerickel commented May 9, 2017

Sorry I meant pop in my example... the issue is that the pop function call is significantly slower than the if foo in bar test just because of python being python and that annoys me because the hot route is a falsey test here but it's fine. Ignore I said anything.

@russellballestrini
Copy link
Contributor Author

@mmerickel - the resource_url function is for traversal and thats hard for me to reason about. I'm still up for the challenge but it will take me a bit longer.

@mmerickel
Copy link
Member

@russellballestrini it's the same code it's just the parameters have different names (no leading underscores).

@mmerickel
Copy link
Member

I went ahead and did the work to cleanup and standardize this effort with resource_url. I only disagree with one part of this change and as such I reverted it. I do like the addition of the test to only prefix the anchor with # if the anchor doesn't already start with #. While # isn't technically allowed in an anchor, I do not like the precedent this would set and the inconsistency it would allow with other parts of the API. Guessing what the user wants here feels odd and there were no added docs to back it up.

@mmerickel
Copy link
Member

mmerickel commented Jun 18, 2017

Found a bug while adding a test... yup. _anchor=None would cause a type error still. Fixed now.

I accidentally pushed this PR as a branch on pyramid which is why the CI tests are messing up but the /pr ones are the ones we care about and they're passing.

Copy link
Contributor Author

@russellballestrini russellballestrini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool - I always love a change with more deletes then adds and testing code is most of the adds!

@mmerickel mmerickel merged commit 5a19501 into Pylons:master Jun 20, 2017
mmerickel added a commit that referenced this pull request Jun 20, 2017
mmerickel added a commit that referenced this pull request Jun 20, 2017
Copy link
Member

@digitalresistor digitalresistor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants