Commit Graph

655 Commits

Author SHA1 Message Date
David Lord 82c2e0366c
remove uses of LocalStack 2022-07-08 11:13:09 -07:00
pgjones 2f1d1d6256 Add further typing tests
This should help ensure the app decorators are correctly typed.
2022-07-08 14:33:20 +01:00
Phil Jones 9b44bf2818
Improve decorator typing (#4676)
* Add a missing setupmethod decorator

* Improve the decorator typing

This will allow type checkers to understand that the decorators return
the same function signature as passed as an argument. This follows the
guidelines from
https://mypy.readthedocs.io/en/stable/generics.html#declaring-decorators.

I've chosen to keep a TypeVar per module and usage as I think
encouraged by PEP 695, which I hope is accepted as the syntax is much
nicer.
2022-07-06 14:05:20 -07:00
David Lord 60b845ebab
update typing tests for json 2022-07-02 21:41:37 -07:00
Grey Li ca2bfbb0ac
Support returning list as JSON 2022-07-02 21:41:32 -07:00
David Lord 1626aff602
Merge branch '2.1.x' 2022-07-02 21:03:09 -07:00
David Lord ab6a8b0330
relax routes cli match order 2022-07-02 21:02:00 -07:00
David Lord 84c722044a
new debug/test preserve context implementation 2022-07-01 12:01:44 -07:00
David Lord 5544d09477
re-add flag to skip unguarded app.run from CLI 2022-06-29 21:19:28 -07:00
pgjones 46433e9807
add generate_template and generate_template_string functions 2022-06-18 12:20:53 -07:00
pgjones 762382e436
view functions can return generators as responses directly 2022-06-18 11:25:05 -07:00
Kevin Kirsche ed42e92928
session expiration datetime is UTC timezone-aware 2022-06-17 12:01:48 -07:00
David Lord c9e000b9ce
with_appcontext lasts for the lifetime of the click context 2022-06-17 11:52:01 -07:00
David Lord ab1fbef29a
prefer --app over FLASK_APP in docs 2022-06-17 09:26:26 -07:00
David Lord aa801c431a
FlaskGroup can be nested 2022-06-15 14:09:56 -07:00
David Lord 6e23239567
add View.init_every_request attribute 2022-06-06 15:11:01 -07:00
David Lord 96c97dec09
deprecate before_first_request 2022-06-06 10:04:05 -07:00
David Lord 7a2d5fb6df
Merge branch '2.1.x' 2022-06-06 09:30:30 -07:00
David Lord b06df0a792
remove outdated instance path test 2022-06-06 09:17:53 -07:00
David Lord 3ba37d2afe
fix uninstalled package tests under tox 2022-06-06 08:24:05 -07:00
Evgeny Prigorodov 88bcf78439
instance_path for namespace packages uses path closest to submodule 2022-06-06 08:16:00 -07:00
David Lord 3351a8677e
add errorhandler type check tests 2022-06-05 15:44:28 -07:00
David Lord 81be290ec8
view function is actually type checked 2022-06-05 15:43:53 -07:00
lecovi 8c6f1d96de
add example code for testing typing tools 2022-06-01 11:34:51 -07:00
Stanislav Bushuev 3ecebcdf8d Add test config.from_mapping method: ignoring items with non-upper keys 2022-05-31 17:51:39 +02:00
David Lord 1888df3440
Merge remote-tracking branch 'origin/2.1.x' 2022-05-23 10:08:52 -07:00
David Lord 1232d69860
inline conditional imports for cli behaviors 2022-05-23 09:46:20 -07:00
David Lord e044b00047
avoid triggering setupmethod late in tests 2022-05-23 09:09:12 -07:00
David Lord 39f9363296
finish moving url_for to app
move entire implementation to app
make special build args actual keyword-only args
handle no app context in method
mention other config in server_name error
implicit external with scheme
use adapter.build url_scheme argument
rewrite documentation
2022-05-14 12:46:01 -07:00
dzcode eb5dd9f5ef
add aborter object to app 2022-05-12 13:29:54 -07:00
Tim Hoagland fdab801fbb
add redirect method to app 2022-05-10 11:02:24 -07:00
Qingpeng Li 1e5dd43022
refactor error checks in register_error_handler
Co-authored-by: David Lord <davidism@gmail.com>
2022-05-03 11:52:11 -06:00
David Lord ef6c2b9e4a
clean up pytest.raises tests 2022-04-28 09:32:31 -07:00
James Warne 5050a18a00
evaluate --cert before --key 2022-04-24 10:01:40 -07:00
David Lord 411dbb37e2
remove old test 2022-04-11 06:11:37 -07:00
David Lord e75d575361
windows env vars are uppercase 2022-03-25 12:07:45 -07:00
David Lord 4eb5e9455b
more from_prefixed_env features
* support nested dict access with "__" separator
* don't specify separator in prefix
* catch exceptions for any loads function
2022-03-25 11:54:45 -07:00
pgjones 08a283af5e
Allow loading of environment variables into the config
This new method will pick out any environment variables with a certain
prefix and place them into the config named without the prefix. This
makes it easy to use environment variables to configure the app as is
now more popular than when Flask started.

The prefix should ensure that the environment isn't polluted and the
config isn't polluted by environment variables.

I've followed the dynaconf convention of trying to parse the
environment variable and then falling back to the raw value if parsing
fails.
2022-03-25 11:54:03 -07:00
David Lord 81c7ff7f8d
update tests for relative redirects 2022-03-25 11:48:26 -07:00
David Lord c9a1f7ad65
don't intercept 307/308 routing redirects
These don't change the request body, so the debug error is no longer relevant.
2022-03-23 08:25:22 -07:00
David Lord e37e87140e
Merge branch '2.0.x' 2022-02-09 07:37:43 -08:00
David Lord 426a1e25b7
fix pytest 7 warnings 2022-02-08 12:26:25 -08:00
David Lord 48f2afbf90
same blueprint cannot be registered with same name 2021-11-12 09:52:16 -08:00
David Lord e21e003f62
remove deprecated script_info factory arg 2021-11-12 09:52:16 -08:00
David Lord e609dddd60
drop Python 3.6 2021-11-11 16:11:43 -08:00
David Lord b2b60450f7
allow lazy loading test to fail on pypy 2021-10-05 09:11:00 -07:00
David Lord 6d65595a3c
try to address flakiness of lazy loading test 2021-10-05 08:03:30 -07:00
Matthias Paulsen 166a2a6207
Fix callback order for nested blueprints
Handlers registered via url_value_preprocessor, before_request,
context_processor, and url_defaults are called in downward order: First
on the app and last on the current blueprint.

Handlers registered via after_request and teardown_request are called
in upward order: First on the current blueprint and last on the app.
2021-10-03 20:27:24 -07:00
na2shell 9f0da9b770
cli loader handles kwargs in app factory 2021-08-05 19:32:43 -07:00
David Lord 2305b056c3
add test for lazy loading error 2021-08-05 19:15:31 -07:00
Rohan salwan aa6dd09c2c
correctly handle raising deferred errors in cli lazy loading 2021-08-05 19:14:51 -07:00
default-303 892ae95509
jsonify encodes decimal to str 2021-08-05 18:36:13 -07:00
pgjones a44c722860 Fix registering a blueprint twice with differing names
Previously the blueprint recorded aspects (before request, after
request etc) would only be added to the app if it was the first
registration of the blueprint instance. However only the record-once
aspects (app-before requests, app-after request) should be added once
on registration of the instance, whereas everything else should be
added on every unique name registration. This ensures that these
trigger under the new name as well as the old.
2021-06-14 17:18:47 +00:00
pre-commit-ci[bot] 5205cd4ea9 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2021-06-01 18:08:19 +00:00
Miguel Grinberg 270eb2df2a Support View and MethodView instances with async handlers 2021-06-01 18:08:19 +00:00
pgjones 3257b7574e Fix blueprint renaming
This ensures that if a blueprint is renamed at the time of
registration that name is used when constructing endpoints, as
expected.
2021-05-21 15:04:30 +01:00
pgjones 714b0a467a Fix blueprint self registration
By raising a ValueError if attempted. I don't see a use case that
makes this worth supporting.
2021-05-21 15:04:30 +01:00
David Lord 9409be6e34 warn when registering same blueprint with same name 2021-05-21 15:04:30 +01:00
pgjones c2920e2bd9 Bugfix allow blueprints to be registered with a different name
This allows the same blueprint to be registered multiple times at the
same level, but with differing url_prefixes and names.
2021-05-21 15:04:18 +01:00
pgjones 99afbb277d Fix blueprint nested url_prefix
This ensures that the url_prefix is correctly applied, no matter if
set during the registration override or when constructing the
blueprint.
2021-05-21 15:04:18 +01:00
Danny Sepler 1b5f21e015
Add pathlib in cli tests 2021-05-17 08:46:36 -07:00
pgjones 6fbdeb80c7 Fix nested blueprint url_prefix
This fixes the case where the blueprint is registered with a
url_prefix but any child blueprints have no prefixes.
2021-05-17 15:47:25 +01:00
David Lord a7b02b3a07
converters have access to session 2021-05-14 08:11:09 -07:00
David Lord 8648750997
Merge branch '1.1.x' into 2.0.x 2021-05-13 18:47:06 -07:00
brettlangdon 3ace642ef3
Use compat fspath instead of os.fspath
When 7ba35c4 was cherry-picked it introduced the
usage of os.fspath which is not supported on
Python <3.6
2021-05-13 18:17:00 -07:00
Grey Li e93704fbfd
fix url_prefix argument when nesting blueprints 2021-05-13 15:02:13 -07:00
David Lord 7c5261407d
blueprint name may not contain a dot 2021-05-13 14:31:50 -07:00
pgjones 2889da67cb
Remove the async helper method
It is better to encourage users to utilise the app ensure_sync method
(or the newely added async_to_sync method) so that any extensions that
alter these methods take affect throughout the users code.

With the helper method users code fix parts of their code to the
asgiref async_to_sync ignoring any extension changes.
2021-05-03 06:18:41 -07:00
pgjones 7f87f3dd93
Simplify the async handling code
Firstly `run_sync` was a misleading name as it didn't run anything,
instead I think `async_to_sync` is much clearer as it converts a
coroutine function to a function. (Name stolen from asgiref).

Secondly trying to run the ensure_sync during registration made the
code more complex and brittle, e.g. the _flask_async_wrapper
usage. This was done to pay any setup costs during registration rather
than runtime, however this only saved a iscoroutne check. It allows
the weirdness of the Blueprint and Scaffold ensure_sync methods to be
removed.

Switching to runtime ensure_sync usage provides a method for
extensions to also support async, as now documented.
2021-05-03 06:15:39 -07:00
Adrian Moennich 26a6cc0f94 Allow using Click 7 with a DeprecationWarning
As long as popular libraries (e.g. Celery) require click 7, depending
on Click 8 in Flask makes it hard to test the latest version (and its
other dependencies) in existing applications.
2021-04-27 16:45:06 +02:00
pgjones 5c6a0f0c12 Fix wrapped view function comparison
Wrapped functions are not comparable, see
https://bugs.python.org/issue3564, therefore a marker is used to note
when the function has been sync wrapped to allow comparison with the
wrapped function instead.

This ensures that multiple route decorators work without raising
exceptions i.e.,

    @app.route("/")
    @app.route("/a")
    async def index():
        ...

works.
2021-04-16 12:34:51 +01:00
pgjones f92e820b4b
Nested blueprints
This allows blueprints to be nested within blueprints via a new
Blueprint.register_blueprint method. This should provide a use case
that has been desired for the past ~10 years.

This works by setting the endpoint name to be the blueprint names,
from parent to child delimeted by "." and then iterating over the
blueprint names in reverse order in the app (from most specific to
most general). This means that the expectation of nesting a blueprint
within a nested blueprint is met.
2021-04-14 09:25:42 -07:00
David Lord 61fbae8664
skip async tests if asgiref isn't installed 2021-04-06 15:33:06 -07:00
pgjones 00f5a3e55c
Alter ensure_sync implementation to support extensions
This allows extensions to override the Flask.ensure_sync method and
have the change apply to blueprints as well. Without this change it is
possible for differing blueprints to have differing ensure_sync
approaches depending on the extension used - which would likely result
in event-loop blocking issues.

This also allows blueprints to have a custom ensure_sync, although
this is a by product rather than an expected use case.
2021-04-06 15:33:06 -07:00
pgjones c6c6408c3f
Raise a runtime error if run_async is called without real ContextVars
Werkzeug offers a ContextVar replacement for Python < 3.7, however it
doesn't work across asyncio tasks, hence it makes sense to error out
rather than find there are odd bugs.

Note the docs build requires the latest (dev) Werkzeug due to this
change (to import ContextVar from werkzeug.local).
2021-04-06 09:35:10 -07:00
pgjones 6979265fa6
Add `async` support
This allows for async functions to be passed to the Flask class
instance, for example as a view function,

    @app.route("/")
    async def index():
        return "Async hello"

this comes with a cost though of poorer performance than using the
sync equivalent.

asgiref is the standard way to run async code within a sync context,
and is used in Django making it a safe and sane choice for this.
2021-04-06 09:35:10 -07:00
Grey Li 33145c3699 Set default encoding to UTF-8 for load_dotenv 2021-03-10 21:40:29 +08:00
pgjones 705e52684a
Add syntatic sugar for route registration
This takes a popular API whereby instead of passing the HTTP method as
an argument to route it is instead used as the method name i.e.

    @app.route("/", methods=["POST"])

is now writeable as,

    @app.post("/")

This is simply syntatic sugar, it doesn't do anything else, but makes
it slightly easier for users.

I've included all the methods that are relevant and aren't auto
generated i.e. not connect, head, options, and trace.
2021-03-08 08:55:14 -08:00
Grey Li 49b7341a49
update json.dumps for http_date changes 2021-02-25 09:45:55 -08:00
David Lord 0ee1b0b5d9
remove test relying on Werkzeug Local internals 2021-02-08 18:04:02 -08:00
David Lord b473e7c97c
use Jinja's tojson filter 2021-02-01 22:48:09 -08:00
Matthew Preble 01621485fd
Ensure session_interface.open_session is called after URL matching (#3776) 2021-02-01 20:41:17 -08:00
Grey Li eb41e7e417 Silence pytest warnings for exception propagation test 2021-01-03 13:57:45 +08:00
David Lord dc11cdb4a4
move send_file and send_from_directory to Werkzeug
The implementations were moved to Werkzeug, Flask's functions become
wrappers around Werkzeug to pass some Flask-specific values.

cache_timeout is renamed to max_age. SEND_FILE_MAX_AGE_DEFAULT,
app.send_file_max_age_default, and app.get_send_file_max_age defaults
to None. This tells the browser to use conditional requests rather than
a 12 hour cache.

attachment_filename is renamed to download_name, and is always sent if
a name is known.

Deprecate helpers.safe_join in favor of werkzeug.utils.safe_join.

Removed most of the send_file tests, they're tested in Werkzeug.

In the file upload example, renamed the uploaded_file view to
download_file to avoid a common source of confusion.
2020-11-05 09:27:52 -08:00
Mathurshan Vimalesvaran 22987b6817
include samesite and secure when removing session cookie (#3726) 2020-11-04 18:16:05 -08:00
David Lord 7a444c5dab
update tests for new werkzeug client (#3827)
Flask's client.open mirrors Werkzeug's for processing an existing
environ.

Always test with latest code for other Pallets projects. This will
be changed back once the new versions are released.
2020-11-04 18:00:21 -08:00
David Lord 373f0dd82e
update requirements (#3823) 2020-11-01 05:30:02 -08:00
David Lord 6def8a4a48
test json.dumps for object with __html__ method 2020-10-31 20:18:25 -07:00
David Lord a0e2aca770
move json tests to separate file 2020-10-31 20:16:24 -07:00
Paul Sanders 8c6baeedab
parametrize some tests (#3786) 2020-10-11 19:16:17 -07:00
Bogdan Opanchuk 8efea0ccbb
Break reference cycle created by default in Flask instances.
Flask instances with static folders were creating a reference cycle
via their "static" view function (which held a strong reference back
to the Flask instance to call its `send_static_file` method). This
prevented CPython from freeing the memory for a Flask instance
when all external references to it were released.

Now use a weakref for the back reference to avoid this.

Co-authored-by: Joshua Bronson <jab@users.noreply.github.com>
2020-10-03 10:05:05 -04:00
David Lord 253570784c
cleaner message when CLI can't load app
When loading the app fails for the --help command, only the error
message is shown, then the help text. The full traceback is shown for
other exceptions. Also show the message when loading fails while
getting a command, instead of only "command not found". The error
message goes to stderr to match other error behavior, and is in red
with an extra newline to make it more obvious next to the help text.

Also fixes an issue with the test_apps fixture that caused an imported
app to still be importable after the test was over and the path was
reset. Now the module cache is reset as well.
2020-07-30 18:36:55 -07:00
David Lord 37551e6798
Merge pull request #3699 from MartinThoma/style
remove unnecessary docstrings from tests
2020-07-28 19:44:23 -07:00
Martin Thoma 75a9b74650
DOC: Remove unnecessary docstrings 2020-07-26 15:47:12 +02:00
Christopher Nguyen 7b09a0904c
change make_response to use headers.update 2020-07-23 16:53:39 -07:00
Joshua Bronson a40c381ffd
Merge pull request #3560 from greyli/fix-env-chdir
Stop change CWD to .env/.flaskenv location
2020-07-18 13:14:09 -04:00
Joshua Bronson b724832872 Cherry-pick 7ba35c4 from master (support pathlib.Path for static_folder) 2020-07-06 13:47:57 -04:00
Joshua Bronson 7ba35c4d4f Restore support for using pathlib.Path for static_folder.
* No longer causes AttributeError: 'PosixPath' object has no
  attribute 'rstrip'.

* This was broken by e6178fe489
  which was released in 1.1.2.

* Add a regression test that now passes.

See #3557.
2020-07-06 08:55:19 -04:00
Grey Li 84cbfc0698 Stop change CWD to .env/.flaskenv location 2020-07-05 09:37:03 +08:00
jackwardell 846ee2c62e
add assert message for errorhandler exception type 2020-04-16 10:49:42 -07:00