From 8c6f1d96ded2a22429b1ee7da6c3870cb140b587 Mon Sep 17 00:00:00 2001 From: lecovi Date: Mon, 2 May 2022 15:22:08 -0300 Subject: [PATCH 1/9] add example code for testing typing tools --- tests/typing/typing_route.py | 53 ++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 tests/typing/typing_route.py diff --git a/tests/typing/typing_route.py b/tests/typing/typing_route.py new file mode 100644 index 00000000..0cb45333 --- /dev/null +++ b/tests/typing/typing_route.py @@ -0,0 +1,53 @@ +from http import HTTPStatus +from typing import Tuple +from typing import Union + +from flask import Flask +from flask import jsonify +from flask.templating import render_template +from flask.views import View +from flask.wrappers import Response + + +app = Flask(__name__) + + +@app.route("/") +def hello_world() -> str: + return "

Hello, World!

" + + +@app.route("/json") +def hello_world_json() -> Response: + return jsonify({"response": "Hello, World!"}) + + +@app.route("/template") +@app.route("/template/") +def return_template(name: Union[str, None] = None) -> str: + return render_template("index.html", name=name) + + +@app.errorhandler(HTTPStatus.INTERNAL_SERVER_ERROR) +def error_500(e) -> Tuple[str, int]: + return "

Sorry, we are having problems

", HTTPStatus.INTERNAL_SERVER_ERROR + + +@app.before_request +def before_request() -> None: + app.logger.debug("Executing a sample before_request function") + return None + + +class RenderTemplateView(View): + def __init__(self: "RenderTemplateView", template_name: str) -> None: + self.template_name = template_name + + def dispatch_request(self: "RenderTemplateView") -> str: + return render_template(self.template_name) + + +app.add_url_rule( + "/about", + view_func=RenderTemplateView.as_view("about_page", template_name="about.html"), +) From 5d31ce1031e8ca24dc908c319567a76110edd87e Mon Sep 17 00:00:00 2001 From: Nick Kocharhook Date: Wed, 1 Jun 2022 12:16:21 -0700 Subject: [PATCH 2/9] Fix incorrect references to query in testing doc The [EnvironBuilder doc](https://werkzeug.palletsprojects.com/en/2.1.x/test/#werkzeug.test.EnvironBuilder) shows that the correct name for the keyword argument is `query_string`, not `query`. Using `query` results in an error. I've fixed the two places this appears in the testing doc. --- docs/testing.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/testing.rst b/docs/testing.rst index 6f9d6ee1..8545bd39 100644 --- a/docs/testing.rst +++ b/docs/testing.rst @@ -92,7 +92,7 @@ The ``client`` has methods that match the common HTTP request methods, such as ``client.get()`` and ``client.post()``. They take many arguments for building the request; you can find the full documentation in :class:`~werkzeug.test.EnvironBuilder`. Typically you'll use ``path``, -``query``, ``headers``, and ``data`` or ``json``. +``query_string``, ``headers``, and ``data`` or ``json``. To make a request, call the method the request should use with the path to the route to test. A :class:`~werkzeug.test.TestResponse` is returned @@ -108,9 +108,9 @@ provides ``response.text``, or use ``response.get_data(as_text=True)``. assert b"

Hello, World!

" in response.data -Pass a dict ``query={"key": "value", ...}`` to set arguments in the -query string (after the ``?`` in the URL). Pass a dict ``headers={}`` -to set request headers. +Pass a dict ``query_string={"key": "value", ...}`` to set arguments in +the query string (after the ``?`` in the URL). Pass a dict +``headers={}`` to set request headers. To send a request body in a POST or PUT request, pass a value to ``data``. If raw bytes are passed, that exact body is used. Usually, From 72cae9ce2b6a5a296b7328df8dd73240e14718e6 Mon Sep 17 00:00:00 2001 From: Numerlor Date: Sun, 5 Jun 2022 02:57:49 +0200 Subject: [PATCH 3/9] Remove extra backtick --- src/flask/scaffold.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/flask/scaffold.py b/src/flask/scaffold.py index 7cd8bab5..e0eab54f 100644 --- a/src/flask/scaffold.py +++ b/src/flask/scaffold.py @@ -123,7 +123,7 @@ class Scaffold: self.view_functions: t.Dict[str, t.Callable] = {} #: A data structure of registered error handlers, in the format - #: ``{scope: {code: {class: handler}}}```. The ``scope`` key is + #: ``{scope: {code: {class: handler}}}``. The ``scope`` key is #: the name of a blueprint the handlers are active for, or #: ``None`` for all requests. The ``code`` key is the HTTP #: status code for ``HTTPException``, or ``None`` for From 81be290ec8889c157c84bc7ce857f883396c5daf Mon Sep 17 00:00:00 2001 From: David Lord Date: Fri, 3 Jun 2022 12:54:54 -0700 Subject: [PATCH 4/9] view function is actually type checked --- src/flask/app.py | 4 +-- src/flask/blueprints.py | 2 +- src/flask/scaffold.py | 30 +++++++++++++++------- src/flask/typing.py | 25 ++++++++---------- tests/typing/typing_route.py | 49 +++++++++++++++++++++--------------- 5 files changed, 64 insertions(+), 46 deletions(-) diff --git a/src/flask/app.py b/src/flask/app.py index 34ea5b29..6b549188 100644 --- a/src/flask/app.py +++ b/src/flask/app.py @@ -1033,7 +1033,7 @@ class Flask(Scaffold): self, rule: str, endpoint: t.Optional[str] = None, - view_func: t.Optional[t.Callable] = None, + view_func: t.Optional[ft.ViewCallable] = None, provide_automatic_options: t.Optional[bool] = None, **options: t.Any, ) -> None: @@ -1681,7 +1681,7 @@ class Flask(Scaffold): if isinstance(rv[1], (Headers, dict, tuple, list)): rv, headers = rv else: - rv, status = rv # type: ignore[misc] + rv, status = rv # type: ignore[assignment,misc] # other sized tuples are not allowed else: raise TypeError( diff --git a/src/flask/blueprints.py b/src/flask/blueprints.py index c60183fa..cf04cd78 100644 --- a/src/flask/blueprints.py +++ b/src/flask/blueprints.py @@ -384,7 +384,7 @@ class Blueprint(Scaffold): self, rule: str, endpoint: t.Optional[str] = None, - view_func: t.Optional[t.Callable] = None, + view_func: t.Optional[ft.ViewCallable] = None, provide_automatic_options: t.Optional[bool] = None, **options: t.Any, ) -> None: diff --git a/src/flask/scaffold.py b/src/flask/scaffold.py index 7cd8bab5..8400e892 100644 --- a/src/flask/scaffold.py +++ b/src/flask/scaffold.py @@ -363,48 +363,60 @@ class Scaffold: method: str, rule: str, options: dict, - ) -> t.Callable[[F], F]: + ) -> t.Callable[[ft.RouteDecorator], ft.RouteDecorator]: if "methods" in options: raise TypeError("Use the 'route' decorator to use the 'methods' argument.") return self.route(rule, methods=[method], **options) - def get(self, rule: str, **options: t.Any) -> t.Callable[[F], F]: + def get( + self, rule: str, **options: t.Any + ) -> t.Callable[[ft.RouteDecorator], ft.RouteDecorator]: """Shortcut for :meth:`route` with ``methods=["GET"]``. .. versionadded:: 2.0 """ return self._method_route("GET", rule, options) - def post(self, rule: str, **options: t.Any) -> t.Callable[[F], F]: + def post( + self, rule: str, **options: t.Any + ) -> t.Callable[[ft.RouteDecorator], ft.RouteDecorator]: """Shortcut for :meth:`route` with ``methods=["POST"]``. .. versionadded:: 2.0 """ return self._method_route("POST", rule, options) - def put(self, rule: str, **options: t.Any) -> t.Callable[[F], F]: + def put( + self, rule: str, **options: t.Any + ) -> t.Callable[[ft.RouteDecorator], ft.RouteDecorator]: """Shortcut for :meth:`route` with ``methods=["PUT"]``. .. versionadded:: 2.0 """ return self._method_route("PUT", rule, options) - def delete(self, rule: str, **options: t.Any) -> t.Callable[[F], F]: + def delete( + self, rule: str, **options: t.Any + ) -> t.Callable[[ft.RouteDecorator], ft.RouteDecorator]: """Shortcut for :meth:`route` with ``methods=["DELETE"]``. .. versionadded:: 2.0 """ return self._method_route("DELETE", rule, options) - def patch(self, rule: str, **options: t.Any) -> t.Callable[[F], F]: + def patch( + self, rule: str, **options: t.Any + ) -> t.Callable[[ft.RouteDecorator], ft.RouteDecorator]: """Shortcut for :meth:`route` with ``methods=["PATCH"]``. .. versionadded:: 2.0 """ return self._method_route("PATCH", rule, options) - def route(self, rule: str, **options: t.Any) -> t.Callable[[F], F]: + def route( + self, rule: str, **options: t.Any + ) -> t.Callable[[ft.RouteDecorator], ft.RouteDecorator]: """Decorate a view function to register it with the given URL rule and options. Calls :meth:`add_url_rule`, which has more details about the implementation. @@ -428,7 +440,7 @@ class Scaffold: :class:`~werkzeug.routing.Rule` object. """ - def decorator(f: F) -> F: + def decorator(f: ft.RouteDecorator) -> ft.RouteDecorator: endpoint = options.pop("endpoint", None) self.add_url_rule(rule, endpoint, f, **options) return f @@ -440,7 +452,7 @@ class Scaffold: self, rule: str, endpoint: t.Optional[str] = None, - view_func: t.Optional[t.Callable] = None, + view_func: t.Optional[ft.ViewCallable] = None, provide_automatic_options: t.Optional[bool] = None, **options: t.Any, ) -> None: diff --git a/src/flask/typing.py b/src/flask/typing.py index ec7c7969..d463a8de 100644 --- a/src/flask/typing.py +++ b/src/flask/typing.py @@ -1,37 +1,30 @@ import typing as t - if t.TYPE_CHECKING: from _typeshed.wsgi import WSGIApplication # noqa: F401 from werkzeug.datastructures import Headers # noqa: F401 from werkzeug.wrappers import Response # noqa: F401 # The possible types that are directly convertible or are a Response object. -ResponseValue = t.Union[ - "Response", - str, - bytes, - t.Dict[str, t.Any], # any jsonify-able dict - t.Iterator[str], - t.Iterator[bytes], -] -StatusCode = int +ResponseValue = t.Union["Response", str, bytes, t.Dict[str, t.Any]] # the possible types for an individual HTTP header -HeaderName = str +# This should be a Union, but mypy doesn't pass unless it's a TypeVar. HeaderValue = t.Union[str, t.List[str], t.Tuple[str, ...]] # the possible types for HTTP headers HeadersValue = t.Union[ - "Headers", t.Dict[HeaderName, HeaderValue], t.List[t.Tuple[HeaderName, HeaderValue]] + "Headers", + t.Mapping[str, HeaderValue], + t.Sequence[t.Tuple[str, HeaderValue]], ] # The possible types returned by a route function. ResponseReturnValue = t.Union[ ResponseValue, t.Tuple[ResponseValue, HeadersValue], - t.Tuple[ResponseValue, StatusCode], - t.Tuple[ResponseValue, StatusCode, HeadersValue], + t.Tuple[ResponseValue, int], + t.Tuple[ResponseValue, int, HeadersValue], "WSGIApplication", ] @@ -51,6 +44,7 @@ TemplateGlobalCallable = t.Callable[..., t.Any] TemplateTestCallable = t.Callable[..., bool] URLDefaultCallable = t.Callable[[str, dict], None] URLValuePreprocessorCallable = t.Callable[[t.Optional[str], t.Optional[dict]], None] + # This should take Exception, but that either breaks typing the argument # with a specific exception, or decorating multiple times with different # exceptions (and using a union type on the argument). @@ -58,3 +52,6 @@ URLValuePreprocessorCallable = t.Callable[[t.Optional[str], t.Optional[dict]], N # https://github.com/pallets/flask/issues/4295 # https://github.com/pallets/flask/issues/4297 ErrorHandlerCallable = t.Callable[[t.Any], ResponseReturnValue] + +ViewCallable = t.Callable[..., ResponseReturnValue] +RouteDecorator = t.TypeVar("RouteDecorator", bound=ViewCallable) diff --git a/tests/typing/typing_route.py b/tests/typing/typing_route.py index 0cb45333..ba49d132 100644 --- a/tests/typing/typing_route.py +++ b/tests/typing/typing_route.py @@ -1,6 +1,6 @@ +from __future__ import annotations + from http import HTTPStatus -from typing import Tuple -from typing import Union from flask import Flask from flask import jsonify @@ -8,42 +8,51 @@ from flask.templating import render_template from flask.views import View from flask.wrappers import Response - app = Flask(__name__) -@app.route("/") -def hello_world() -> str: +@app.route("/str") +def hello_str() -> str: return "

Hello, World!

" +@app.route("/bytes") +def hello_bytes() -> bytes: + return b"

Hello, World!

" + + @app.route("/json") -def hello_world_json() -> Response: +def hello_json() -> Response: return jsonify({"response": "Hello, World!"}) +@app.route("/status") +@app.route("/status/") +def tuple_status(code: int = 200) -> tuple[str, int]: + return "hello", code + + +@app.route("/status-enum") +def tuple_status_enum() -> tuple[str, int]: + return "hello", HTTPStatus.OK + + +@app.route("/headers") +def tuple_headers() -> tuple[str, dict[str, str]]: + return "Hello, World!", {"Content-Type": "text/plain"} + + @app.route("/template") @app.route("/template/") -def return_template(name: Union[str, None] = None) -> str: +def return_template(name: str | None = None) -> str: return render_template("index.html", name=name) -@app.errorhandler(HTTPStatus.INTERNAL_SERVER_ERROR) -def error_500(e) -> Tuple[str, int]: - return "

Sorry, we are having problems

", HTTPStatus.INTERNAL_SERVER_ERROR - - -@app.before_request -def before_request() -> None: - app.logger.debug("Executing a sample before_request function") - return None - - class RenderTemplateView(View): - def __init__(self: "RenderTemplateView", template_name: str) -> None: + def __init__(self: RenderTemplateView, template_name: str) -> None: self.template_name = template_name - def dispatch_request(self: "RenderTemplateView") -> str: + def dispatch_request(self: RenderTemplateView) -> str: return render_template(self.template_name) From 3351a8677e5bd1e1b8cfe2860b73b3422cde4f31 Mon Sep 17 00:00:00 2001 From: David Lord Date: Sun, 5 Jun 2022 15:44:28 -0700 Subject: [PATCH 5/9] add errorhandler type check tests --- src/flask/blueprints.py | 6 +++-- src/flask/scaffold.py | 4 ++-- src/flask/typing.py | 1 + tests/typing/typing_error_handler.py | 33 ++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 tests/typing/typing_error_handler.py diff --git a/src/flask/blueprints.py b/src/flask/blueprints.py index cf04cd78..87617989 100644 --- a/src/flask/blueprints.py +++ b/src/flask/blueprints.py @@ -561,12 +561,14 @@ class Blueprint(Scaffold): ) return f - def app_errorhandler(self, code: t.Union[t.Type[Exception], int]) -> t.Callable: + def app_errorhandler( + self, code: t.Union[t.Type[Exception], int] + ) -> t.Callable[[ft.ErrorHandlerDecorator], ft.ErrorHandlerDecorator]: """Like :meth:`Flask.errorhandler` but for a blueprint. This handler is used for all requests, even if outside of the blueprint. """ - def decorator(f: ft.ErrorHandlerCallable) -> ft.ErrorHandlerCallable: + def decorator(f: ft.ErrorHandlerDecorator) -> ft.ErrorHandlerDecorator: self.record_once(lambda s: s.app.errorhandler(code)(f)) return f diff --git a/src/flask/scaffold.py b/src/flask/scaffold.py index 8400e892..dedfe309 100644 --- a/src/flask/scaffold.py +++ b/src/flask/scaffold.py @@ -654,7 +654,7 @@ class Scaffold: @setupmethod def errorhandler( self, code_or_exception: t.Union[t.Type[Exception], int] - ) -> t.Callable[[ft.ErrorHandlerCallable], ft.ErrorHandlerCallable]: + ) -> t.Callable[[ft.ErrorHandlerDecorator], ft.ErrorHandlerDecorator]: """Register a function to handle errors by code or exception class. A decorator that is used to register a function given an @@ -684,7 +684,7 @@ class Scaffold: an arbitrary exception """ - def decorator(f: ft.ErrorHandlerCallable) -> ft.ErrorHandlerCallable: + def decorator(f: ft.ErrorHandlerDecorator) -> ft.ErrorHandlerDecorator: self.register_error_handler(code_or_exception, f) return f diff --git a/src/flask/typing.py b/src/flask/typing.py index d463a8de..e6d67f20 100644 --- a/src/flask/typing.py +++ b/src/flask/typing.py @@ -52,6 +52,7 @@ URLValuePreprocessorCallable = t.Callable[[t.Optional[str], t.Optional[dict]], N # https://github.com/pallets/flask/issues/4295 # https://github.com/pallets/flask/issues/4297 ErrorHandlerCallable = t.Callable[[t.Any], ResponseReturnValue] +ErrorHandlerDecorator = t.TypeVar("ErrorHandlerDecorator", bound=ErrorHandlerCallable) ViewCallable = t.Callable[..., ResponseReturnValue] RouteDecorator = t.TypeVar("RouteDecorator", bound=ViewCallable) diff --git a/tests/typing/typing_error_handler.py b/tests/typing/typing_error_handler.py new file mode 100644 index 00000000..ec9c886f --- /dev/null +++ b/tests/typing/typing_error_handler.py @@ -0,0 +1,33 @@ +from __future__ import annotations + +from http import HTTPStatus + +from werkzeug.exceptions import BadRequest +from werkzeug.exceptions import NotFound + +from flask import Flask + +app = Flask(__name__) + + +@app.errorhandler(400) +@app.errorhandler(HTTPStatus.BAD_REQUEST) +@app.errorhandler(BadRequest) +def handle_400(e: BadRequest) -> str: + return "" + + +@app.errorhandler(ValueError) +def handle_custom(e: ValueError) -> str: + return "" + + +@app.errorhandler(ValueError) +def handle_accept_base(e: Exception) -> str: + return "" + + +@app.errorhandler(BadRequest) +@app.errorhandler(404) +def handle_multiple(e: BadRequest | NotFound) -> str: + return "" From 48766754b8e8a16bfb075229eaaf88643c958c4e Mon Sep 17 00:00:00 2001 From: David Lord Date: Sun, 5 Jun 2022 15:49:41 -0700 Subject: [PATCH 6/9] add typing tests to mypy config --- setup.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index 597eece1..e858d13a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -87,7 +87,7 @@ per-file-ignores = src/flask/__init__.py: F401 [mypy] -files = src/flask +files = src/flask, tests/typing python_version = 3.7 show_error_codes = True allow_redefinition = True From 88bcf78439b66b5fcba9f4d3a1c56307f98dbf1d Mon Sep 17 00:00:00 2001 From: Evgeny Prigorodov Date: Thu, 26 May 2022 21:12:36 +0200 Subject: [PATCH 7/9] instance_path for namespace packages uses path closest to submodule --- CHANGES.rst | 2 ++ src/flask/scaffold.py | 39 ++++++++++++++++++++++++----------- tests/test_instance_config.py | 19 +++++++++++++++++ 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index a181badc..d423aba9 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -8,6 +8,8 @@ Unreleased - Inline some optional imports that are only used for certain CLI commands. :pr:`4606` - Relax type annotation for ``after_request`` functions. :issue:`4600` +- ``instance_path`` for namespace packages uses the path closest to + the imported submodule. :issue:`4600` Version 2.1.2 diff --git a/src/flask/scaffold.py b/src/flask/scaffold.py index 8ca804a6..147d9827 100644 --- a/src/flask/scaffold.py +++ b/src/flask/scaffold.py @@ -780,30 +780,46 @@ def _matching_loader_thinks_module_is_package(loader, mod_name): ) -def _find_package_path(root_mod_name): +def _find_package_path(import_name): """Find the path that contains the package or module.""" - try: - spec = importlib.util.find_spec(root_mod_name) + root_mod_name, _, _ = import_name.partition(".") - if spec is None: + try: + root_spec = importlib.util.find_spec(root_mod_name) + + if root_spec is None: raise ValueError("not found") # ImportError: the machinery told us it does not exist # ValueError: # - the module name was invalid # - the module name is __main__ - # - *we* raised `ValueError` due to `spec` being `None` + # - *we* raised `ValueError` due to `root_spec` being `None` except (ImportError, ValueError): pass # handled below else: # namespace package - if spec.origin in {"namespace", None}: - return os.path.dirname(next(iter(spec.submodule_search_locations))) + if root_spec.origin in {"namespace", None}: + package_spec = importlib.util.find_spec(import_name) + if package_spec is not None and package_spec.submodule_search_locations: + # Pick the path in the namespace that contains the submodule. + package_path = os.path.commonpath( + package_spec.submodule_search_locations + ) + search_locations = ( + location + for location in root_spec.submodule_search_locations + if package_path.startswith(location) + ) + else: + # Pick the first path. + search_locations = iter(root_spec.submodule_search_locations) + return os.path.dirname(next(search_locations)) # a package (with __init__.py) - elif spec.submodule_search_locations: - return os.path.dirname(os.path.dirname(spec.origin)) + elif root_spec.submodule_search_locations: + return os.path.dirname(os.path.dirname(root_spec.origin)) # just a normal module else: - return os.path.dirname(spec.origin) + return os.path.dirname(root_spec.origin) # we were unable to find the `package_path` using PEP 451 loaders loader = pkgutil.get_loader(root_mod_name) @@ -845,8 +861,7 @@ def find_package(import_name: str): for import. If the package is not installed, it's assumed that the package was imported from the current working directory. """ - root_mod_name, _, _ = import_name.partition(".") - package_path = _find_package_path(root_mod_name) + package_path = _find_package_path(import_name) py_prefix = os.path.abspath(sys.prefix) # installed to the system diff --git a/tests/test_instance_config.py b/tests/test_instance_config.py index ee573664..d7fe6191 100644 --- a/tests/test_instance_config.py +++ b/tests/test_instance_config.py @@ -59,6 +59,25 @@ def test_uninstalled_package_paths(modules_tmpdir, purge_module): assert app.instance_path == str(modules_tmpdir.join("instance")) +def test_uninstalled_namespace_paths(tmpdir, monkeypatch, purge_module): + def create_namespace(package): + project = tmpdir.join(f"project-{package}") + monkeypatch.syspath_prepend(str(project)) + project.join("namespace").join(package).join("__init__.py").write( + "import flask\napp = flask.Flask(__name__)\n", ensure=True + ) + return project + + _ = create_namespace("package1") + project2 = create_namespace("package2") + purge_module("namespace.package2") + purge_module("namespace") + + from namespace.package2 import app + + assert app.instance_path == str(project2.join("instance")) + + def test_installed_module_paths( modules_tmpdir, modules_tmpdir_prefix, purge_module, site_packages, limit_loader ): From 3ba37d2afe6511c3f3153248f7342174bea5b131 Mon Sep 17 00:00:00 2001 From: David Lord Date: Mon, 6 Jun 2022 08:24:05 -0700 Subject: [PATCH 8/9] fix uninstalled package tests under tox --- src/flask/scaffold.py | 18 ++++++++++++++---- tests/test_instance_config.py | 3 --- tox.ini | 1 + 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/flask/scaffold.py b/src/flask/scaffold.py index 147d9827..80084a19 100644 --- a/src/flask/scaffold.py +++ b/src/flask/scaffold.py @@ -1,5 +1,6 @@ import importlib.util import os +import pathlib import pkgutil import sys import typing as t @@ -780,6 +781,15 @@ def _matching_loader_thinks_module_is_package(loader, mod_name): ) +def _path_is_relative_to(path: pathlib.PurePath, base: str) -> bool: + # Path.is_relative_to doesn't exist until Python 3.9 + try: + path.relative_to(base) + return True + except ValueError: + return False + + def _find_package_path(import_name): """Find the path that contains the package or module.""" root_mod_name, _, _ = import_name.partition(".") @@ -802,13 +812,13 @@ def _find_package_path(import_name): package_spec = importlib.util.find_spec(import_name) if package_spec is not None and package_spec.submodule_search_locations: # Pick the path in the namespace that contains the submodule. - package_path = os.path.commonpath( - package_spec.submodule_search_locations + package_path = pathlib.Path( + os.path.commonpath(package_spec.submodule_search_locations) ) search_locations = ( location for location in root_spec.submodule_search_locations - if package_path.startswith(location) + if _path_is_relative_to(package_path, location) ) else: # Pick the first path. @@ -865,7 +875,7 @@ def find_package(import_name: str): py_prefix = os.path.abspath(sys.prefix) # installed to the system - if package_path.startswith(py_prefix): + if _path_is_relative_to(pathlib.PurePath(package_path), py_prefix): return py_prefix, package_path site_parent, site_folder = os.path.split(package_path) diff --git a/tests/test_instance_config.py b/tests/test_instance_config.py index d7fe6191..53e98042 100644 --- a/tests/test_instance_config.py +++ b/tests/test_instance_config.py @@ -15,7 +15,6 @@ def test_explicit_instance_paths(modules_tmpdir): assert app.instance_path == str(modules_tmpdir) -@pytest.mark.xfail(reason="weird interaction with tox") def test_main_module_paths(modules_tmpdir, purge_module): app = modules_tmpdir.join("main_app.py") app.write('import flask\n\napp = flask.Flask("__main__")') @@ -27,7 +26,6 @@ def test_main_module_paths(modules_tmpdir, purge_module): assert app.instance_path == os.path.join(here, "instance") -@pytest.mark.xfail(reason="weird interaction with tox") def test_uninstalled_module_paths(modules_tmpdir, purge_module): app = modules_tmpdir.join("config_module_app.py").write( "import os\n" @@ -42,7 +40,6 @@ def test_uninstalled_module_paths(modules_tmpdir, purge_module): assert app.instance_path == str(modules_tmpdir.join("instance")) -@pytest.mark.xfail(reason="weird interaction with tox") def test_uninstalled_package_paths(modules_tmpdir, purge_module): app = modules_tmpdir.mkdir("config_package_app") init = app.join("__init__.py") diff --git a/tox.ini b/tox.ini index 077d66f2..ee4d40f6 100644 --- a/tox.ini +++ b/tox.ini @@ -9,6 +9,7 @@ envlist = skip_missing_interpreters = true [testenv] +envtmpdir = {toxworkdir}/tmp/{envname} deps = -r requirements/tests.txt min: -r requirements/tests-pallets-min.txt From b06df0a792ceb5506da47e0c8fea09902c1058f9 Mon Sep 17 00:00:00 2001 From: David Lord Date: Mon, 6 Jun 2022 09:17:53 -0700 Subject: [PATCH 9/9] remove outdated instance path test --- tests/test_instance_config.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/tests/test_instance_config.py b/tests/test_instance_config.py index 53e98042..c8cf0bf7 100644 --- a/tests/test_instance_config.py +++ b/tests/test_instance_config.py @@ -1,4 +1,3 @@ -import os import sys import pytest @@ -15,17 +14,6 @@ def test_explicit_instance_paths(modules_tmpdir): assert app.instance_path == str(modules_tmpdir) -def test_main_module_paths(modules_tmpdir, purge_module): - app = modules_tmpdir.join("main_app.py") - app.write('import flask\n\napp = flask.Flask("__main__")') - purge_module("main_app") - - from main_app import app - - here = os.path.abspath(os.getcwd()) - assert app.instance_path == os.path.join(here, "instance") - - def test_uninstalled_module_paths(modules_tmpdir, purge_module): app = modules_tmpdir.join("config_module_app.py").write( "import os\n"