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

updated to work with flask 2.2+ #183

Merged
merged 3 commits into from
Oct 4, 2022
Merged

updated to work with flask 2.2+ #183

merged 3 commits into from
Oct 4, 2022

Conversation

christopherpickering
Copy link
Contributor

Replaced _request_ctx_stack.top with request_ctx per https://github.com/pallets/flask/pull/4682/files

Replaced _request_ctx_stack.top with request_ctx per https://github.com/pallets/flask/pull/4682/files
@nickjj
Copy link
Contributor

nickjj commented Aug 18, 2022

Hi,

I'm using the currently released version of DTB in a Flask 2.2+ app and it's working here. Did you encounter an error?

@christopherpickering
Copy link
Contributor Author

@nickjj, hey, no, no errors, just the depreciation warning that in flask 2.3 the _request_ctx_stack will be removed.

@nickjj
Copy link
Contributor

nickjj commented Aug 18, 2022

Ah right.

This project currently has Flask >= 0.8 defined. I do like the idea of removing the deprecation warning by using the "new" way for Flask 2.3 but we should figure out a solution that still works for versions < 2.3. I think you could detect the version of Flask and import either one based on the version and use as to ensure the import name is the same for both so the usage can remain as req = request_ctx.request.

What do you think?

@christopherpickering
Copy link
Contributor Author

Yeah, that's an idea. From the flask PR, they said this was existing all along, but I'm not sure what version of flask it was really added.

We could something like this:
int(flask.version.split(".")[0]) < 2 and int(flask.version.split(".")[1]) < 2

Probably there is a cleaner way?

@nickjj
Copy link
Contributor

nickjj commented Aug 18, 2022

I didn't fully test this but if you put this into a Python interpreter it works. The packaging library is part of Python's standard library:

import flask

from packaging import version

version.parse(flask.__version__) >= version.parse("2.2.0")

That condition is what you could do to require a specific package from Flask.

@christopherpickering
Copy link
Contributor Author

How this look @nickjj ?

The tests run fine w/ tox, but not the stylecheck. It looks like the tox file needs updated?

@nickjj nickjj mentioned this pull request Aug 19, 2022
@jeffwidman
Copy link
Member

jeffwidman commented Sep 18, 2022

I filed #184 to track bumping our version of Flask... I'm very open to doing so if /when we need it. But as @nickjj pointed out here, for this PR checking the version is a clean-backwards compatible method, so 👍 for that here.

Copy link
Member

@jeffwidman jeffwidman left a comment

Choose a reason for hiding this comment

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

LGTM, but @nickjj can you take a quick look too? If looks good to you, please merge...

if version_builder.parse(flask.__version__) >= version_builder.parse("2.2.0"):
req = request_ctx.request
else:
req = _request_ctx_stack.top.request
Copy link
Member

Choose a reason for hiding this comment

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

Oh I see, you can't just alias the package import (from flask.globals import _request_ctx_stack as request_ctx) because the object's API changed as well...

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep I discovered that after the comment too.

from flask import Blueprint, current_app, request, g, send_from_directory, url_for
from flask.globals import _request_ctx_stack


Copy link
Member

Choose a reason for hiding this comment

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

I don't work much with Python these days, but I thought style guides would suggest this as only 1 blank line rather than 2 between the other imports (or even no blank lines since it's just a conditional import and not a distinct function)? Which does pycodestyle prefer?

Copy link
Contributor

Choose a reason for hiding this comment

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

Generally black prefers 2 lines between functions but there's no set rule for spaces between imports and your first expressions. This is also a fun one because it's technically handling an import but it's also an expression.

Do you have a preference? I don't have pycodestyle installed locally.

Copy link
Member

Choose a reason for hiding this comment

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

I'm cool either way!

@nickjj
Copy link
Contributor

nickjj commented Sep 18, 2022

I'm generally ok with it as a concept. I'm not loving repeating that version check twice but this also feels like a spot where if it were refactored and made into a general version compare function it might make things less readable in the end.

Do you want to wait for the author of the PR to potentially remove that 1 empty line before merging it @jeffwidman?

@jeffwidman jeffwidman merged commit 890fd9c into pallets-eco:master Oct 4, 2022
@jeffwidman
Copy link
Member

Thanks again!

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

Successfully merging this pull request may close these issues.

3 participants