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

Fix: Make error initializers more consistent #1655

Merged
merged 29 commits into from Feb 15, 2020

Conversation

CaselIT
Copy link
Member

@CaselIT CaselIT commented Jan 25, 2020

Summary of Changes

Normalized the interface of the http errors with common title and description kwargs.
Compared to the issue I've left the headers kw as is, since it's used in all errors

Related Issues

Fixes: #777

Pull Request Checklist

This is just a reminder about the most common mistakes. Please make sure that you tick all appropriate boxes. But please read our contribution guide at least once; it will save you a few review cycles!

If an item doesn't apply to your pull request, check it anyway to make it apparent that there's nothing to do.

  • Added tests for changed code.
  • Prefixed code comments with GitHub nick and an appropriate prefix.
  • Coding style is consistent with the rest of the framework.
  • Updated documentation for changed code.
    • Added docstrings for any new classes, functions, or modules.
    • Updated docstrings for any modifications to existing code.
    • Added references to new classes, functions, or modules to the relevant RST file under docs/.
    • Updated all relevant supporting documentation files under docs/.
    • A copyright notice is included at the top of any new modules (using your own name or the name of your organization).
    • Changed/added classes/methods/functions have appropriate versionadded, versionchanged, or deprecated directives.
  • Changes (and possible deprecations) have towncrier news fragments under docs/_newsfragments/. (Run towncrier --draft to ensure it renders correctly.)

If you have any questions to any of the points above, just submit and ask! This checklist is here to help you, not to deter you from contributing!

PR template inspired by the attrs project.

@codecov
Copy link

codecov bot commented Jan 25, 2020

Codecov Report

❗ No coverage uploaded for pull request base (master@769de42). Click here to learn what that means.
The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff            @@
##             master   #1655   +/-   ##
========================================
  Coverage          ?    100%           
========================================
  Files             ?      47           
  Lines             ?    3565           
  Branches          ?     545           
========================================
  Hits              ?    3565           
  Misses            ?       0           
  Partials          ?       0
Impacted Files Coverage Δ
falcon/constants.py 100% <100%> (ø)
falcon/util/misc.py 100% <100%> (ø)
falcon/media/__init__.py 100% <100%> (ø)
falcon/media/handlers.py 100% <100%> (ø)
falcon/util/reader.py 100% <100%> (ø)
falcon/media/multipart.py 100% <100%> (ø)
falcon/util/__init__.py 100% <100%> (ø)
falcon/media/base.py 100% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 769de42...8793055. Read the comment docs.

@CaselIT
Copy link
Member Author

CaselIT commented Jan 25, 2020

I've a couple of questions:

  • should the kw argument be kw only? Using PEP 3102 def __init__(self, *, title=None, ...)?
  • some types have additional keyword args, that are placed in headers, like challenges in HTTPUnauthorized. I've not changed this. Should we change it?

@kgriffs
Copy link
Member

kgriffs commented Jan 25, 2020

should the kw argument be kw only? Using PEP 3102 def init(self, *, title=None, ...)?

Yes, I think that would be a Good Thing™. As an aside, we should consider doing this in other places as well so we don't have to worry about a simple reordering of kwargs down the road causing a breaking change. @vytas7 thoughts?

@kgriffs
Copy link
Member

kgriffs commented Jan 25, 2020

some types have additional keyword args, that are placed in headers, like challenges in HTTPUnauthorized. I've not changed this. Should we change it?

It would not hurt to normalize the ordering of extra params such that they always follow headers (although it would only be cosmetic if PEP 3102 is used). Regardless, we will need to make sure we have test coverage for things like that overriding any values also set in headers.

@@ -377,11 +372,9 @@ class HTTPMethodNotAllowed(OptionalRepresentation, HTTPError):
base articles related to this error (default ``None``).
"""

def __init__(self, allowed_methods, headers=None, **kwargs):
def __init__(self, allowed_methods, title=None, description=None, headers=None, **kwargs):
Copy link
Member

Choose a reason for hiding this comment

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

We'll need a breaking change news fragment since people may have been passing headers positionally.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll add it to the existing one regarding the NoRepresentation. Also the kw only change

Copy link
Member Author

Choose a reason for hiding this comment

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

btw towncrier does not properly render the misc changelog

@CaselIT
Copy link
Member Author

CaselIT commented Jan 25, 2020

we will need to make sure we have test coverage for things like that overriding any values also set in headers.

I think the current implementation overrides in all of them. I can check if that is the case and check if there are tests for it. Sadly the code coverage does not help in this case

@@ -0,0 +1,2 @@
Update to the error classes in ``falcon.errors`` to have ``title`` and
Copy link
Member

Choose a reason for hiding this comment

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

This needs just a slight rewording to be consistent with other fragments:

The error classes in ``falcon.errors`` were updated to have...

@CaselIT
Copy link
Member Author

CaselIT commented Jan 26, 2020

I'm changing to kwonly.

There seem to be a lot of changes required, both in code and in documentations.
Just letting you know

@CaselIT
Copy link
Member Author

CaselIT commented Jan 26, 2020

I've notices that for the headers the documentation says: headers (dict or list)

But the list case is not handled by all the errors that add something to the headers, like HTTPUnauthorized:

falcon/falcon/errors.py

Lines 165 to 170 in c817a42

def __init__(self, title=None, description=None, challenges=None, headers=None, **kwargs):
if headers is None:
headers = {}
if challenges:
headers['WWW-Authenticate'] = ', '.join(challenges)

@CaselIT
Copy link
Member Author

CaselIT commented Jan 26, 2020

should be almost done other than #1655 (comment)

@CaselIT CaselIT requested a review from kgriffs January 26, 2020 14:06
@CaselIT
Copy link
Member Author

CaselIT commented Jan 27, 2020

should the kw argument be kw only? Using PEP 3102 def init(self, *, title=None, ...)?

I know I'm the one who suggested it, and I've already implemented it, but thinking a bit more about it and also judging by the amount of refactoring that this has required in falcon, I'm not sure if it's worth the trouble. By it I mead that it will probably cause a lot of refactoring on the users who use falcon, without a clear improvement.
I think we can keep the changes in falcon, but I would revert the actual requirement for kw_only. Or at least move the * after the title and description.

What do you think @kgriffs @vytas7

@vytas7
Copy link
Member

vytas7 commented Jan 27, 2020

I know I'm the one who suggested it, and I've already implemented it, but thinking a bit more about it and also judging by the amount of refactoring that this has required in falcon, I'm not sure if it's worth the trouble. By it I mead that it will probably cause a lot of refactoring on the users who use falcon, without a clear improvement.

I'd need to check signatures of the existing HTTPErrors & HTTPStatuses more carefully to provide an informed opinion.

A quick reaction off the top of my head would be that it indeed sounds like a bit too abrupt breaking change without any compatibility shims or deprecation warnings, even given that 3.0 will be a major release.

@kgriffs
Copy link
Member

kgriffs commented Feb 3, 2020

IMO we should at least add a warning section to the relevant docstrings. As for outputting a deprecation warning, I'm inclined to say yes as well, since this raising errors is typically not super performance sensitive, and the extra nudge to the app developer can't hurt. @vytas7 thoughts?

@vytas7
Copy link
Member

vytas7 commented Feb 3, 2020

Agreed re the deprecation warning, not many are going to care or even notice about a line or two in the docs. It is ideal to emit them if possible, and agreed re the performance impact being lesser for exceptions.

@CaselIT
Copy link
Member Author

CaselIT commented Feb 3, 2020

ok, so something like the deprecate_arg in the snippet above

@CaselIT
Copy link
Member Author

CaselIT commented Feb 4, 2020

Ok I've replaced the kwonly with a warning and a note in the documents. (not sure if it should be a warning)

I think it's ready for review

@CaselIT CaselIT requested a review from vytas7 February 4, 2020 20:26
Copy link
Member

@kgriffs kgriffs left a comment

Choose a reason for hiding this comment

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

Just a few minor things and this should be ready to merge!

falcon/util/misc.py Show resolved Hide resolved
falcon/util/misc.py Outdated Show resolved Hide resolved
falcon/util/misc.py Outdated Show resolved Hide resolved
falcon/util/misc.py Show resolved Hide resolved
falcon/util/misc.py Outdated Show resolved Hide resolved
kgriffs
kgriffs previously approved these changes Feb 5, 2020
@CaselIT
Copy link
Member Author

CaselIT commented Feb 7, 2020

I've noticed that the changelog needs updating.

@@ -0,0 +1,4 @@
The class :class:`~.falcon.http_error.NoRepresentation` was removed since
all :class:`~.falcon.HTTPError` have optional representation.
Deprecate the use of positional arguments for the optional kw args of
Copy link
Member

Choose a reason for hiding this comment

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

Seems like this deprecation could be its own news fragment?

Copy link
Member Author

@CaselIT CaselIT Feb 9, 2020

Choose a reason for hiding this comment

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

I'm not sure how to name them. They should be 777 since that is the issue, and I think are both removal?

Copy link
Member

Choose a reason for hiding this comment

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

You could make the other one "misc" since it hasn't been removed (as a breaking change) yet, per se. Also, I think it is reasonable to add a few (hyphenated) words of description after the issue number (as has been done with other fragments.

@csojinb @vytas7 thoughts? Related: #1621 (comment)

Copy link
Member Author

@CaselIT CaselIT Feb 11, 2020

Choose a reason for hiding this comment

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

I've used only the numbers as the names since I though that we wanted to use towncrier to automatically add the link to the issue for every changelog file.
If we don't need to only use the number as the name I'll make a new misc file

Copy link
Member

@kgriffs kgriffs Feb 11, 2020

Choose a reason for hiding this comment

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

Ahh, I see. I just tried that and indeed it drops a (#{number}) reference in there, although it doesn't generate a GH link by default. That is nice, but on the other hand it seems like there should be a way to (1) have towncrier allow multiple entries for the same issue and (2) to better support items that don't have an associated issue, perhaps by simply not appending the (name-of-news-fragment). Perhaps we need a post-processing script?

Copy link
Member Author

Choose a reason for hiding this comment

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

There is a configuration parameter for towncrier to make it apply the gh link. We were trying it in gitter with @vytas7

I can just change the name for now, and then the names of the newsitems will be changed after the configuration for towncrier is figured out

Copy link
Member Author

Choose a reason for hiding this comment

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

the config was issue_format = "https://github.com/falconry/falcon/issues/{issue}"

@kgriffs
Copy link
Member

kgriffs commented Feb 9, 2020

I've noticed that the changelog needs updating.

Nice catch! I think we are all trying to get used to making news fragments part of the PRs.

@CaselIT CaselIT requested a review from kgriffs February 11, 2020 19:06
kgriffs
kgriffs previously approved these changes Feb 12, 2020
@kgriffs kgriffs mentioned this pull request Feb 13, 2020
11 tasks
@kgriffs kgriffs merged commit 61d6424 into falconry:master Feb 15, 2020
@CaselIT CaselIT deleted the fix-777-errors branch February 15, 2020 19:54
@vytas7 vytas7 mentioned this pull request Mar 25, 2020
13 tasks
@CaselIT CaselIT mentioned this pull request Apr 16, 2020
13 tasks
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.

Make error initializers more consistent
3 participants