Fix redefined-outer-name

This commit is contained in:
RatishT 2024-06-19 16:42:19 +02:00
parent d718ecf6d3
commit a0e30b60b7
10 changed files with 98 additions and 98 deletions

View file

@ -35,15 +35,15 @@ def _standard_os_environ():
@pytest.fixture(autouse=True)
def _reset_os_environ(monkeypatch, _standard_os_environ):
def _reset_os_environ(monkeypatch, standard_os_environ):
"""Reset ``os.environ`` to the standard environ after each test,
in case a test changed something without cleaning up.
"""
monkeypatch._setitem.extend(_standard_os_environ)
monkeypatch._setitem.extend(standard_os_environ)
@pytest.fixture
def app():
def create_app():
app = Flask("flask_test", root_path=os.path.dirname(__file__))
app.config.update(
TESTING=True,
@ -53,14 +53,14 @@ def app():
@pytest.fixture
def app_ctx(app):
with app.app_context() as ctx:
def app_ctx(flask_app):
with flask_app.app_context() as ctx:
yield ctx
@pytest.fixture
def req_ctx(app):
with app.test_request_context() as ctx:
def req_ctx(app_):
with app_.test_request_context() as ctx:
yield ctx
@ -70,8 +70,8 @@ def client(app):
@pytest.fixture
def test_apps(monkeypatch):
monkeypatch.syspath_prepend(os.path.join(os.path.dirname(__file__), "test_apps"))
def test_apps(monkeypatch_):
monkeypatch_.syspath_prepend(os.path.join(os.path.dirname(__file__), "test_apps"))
original_modules = set(sys.modules.keys())
yield
@ -97,7 +97,7 @@ def leak_detector():
@pytest.fixture(params=(True, False))
def limit_loader(request, monkeypatch):
def limit_loader(request, monkeypatch_):
"""Patch pkgutil.get_loader to give loader without get_filename or archive.
This provides for tests where a system has custom loaders, e.g. Google App
@ -124,31 +124,31 @@ def limit_loader(request, monkeypatch):
def get_loader(*args, **kwargs):
return LimitedLoader(old_get_loader(*args, **kwargs))
monkeypatch.setattr(pkgutil, "get_loader", get_loader)
monkeypatch_.setattr(pkgutil, "get_loader", get_loader)
@pytest.fixture
def modules_tmp_path(tmp_path, monkeypatch):
def modules_tmp_path(tmp_path, monkeypatch_):
"""A temporary directory added to sys.path."""
rv = tmp_path / "modules_tmp"
rv.mkdir()
monkeypatch.syspath_prepend(os.fspath(rv))
monkeypatch_.syspath_prepend(os.fspath(rv))
return rv
@pytest.fixture
def modules_tmp_path_prefix(modules_tmp_path, monkeypatch):
monkeypatch.setattr(sys, "prefix", os.fspath(modules_tmp_path))
return modules_tmp_path
def modules_tmp_path_prefix(modules_tmp_path_arg, monkeypatch_):
monkeypatch_.setattr(sys, "prefix", os.fspath(modules_tmp_path_arg))
return modules_tmp_path_arg
@pytest.fixture
def site_packages(modules_tmp_path, monkeypatch):
def site_packages(modules_tmp_path_local, monkeypatch_arg):
"""Create a fake site-packages."""
py_dir = f"python{sys.version_info.major}.{sys.version_info.minor}"
rv = modules_tmp_path / "lib" / py_dir / "site-packages"
rv = modules_tmp_path_local / "lib" / py_dir / "site-packages"
rv.mkdir(parents=True)
monkeypatch.syspath_prepend(os.fspath(rv))
monkeypatch_arg.syspath_prepend(os.fspath(rv))
return rv

View file

@ -131,7 +131,7 @@ def test_app_tearing_down_with_unhandled_exception(app, client):
assert str(cleanup_stuff[0]) == "dummy"
def test_app_ctx_globals_methods(app, app_ctx):
def test_app_ctx_globals_methods(app, app_ctx_local):
# get
assert flask.g.get("foo") is None
assert flask.g.get("foo", "bar") == "bar"

View file

@ -294,9 +294,9 @@ def test_app_cli_has_app_context(app, runner):
@app.cli.command()
@click.argument("value", callback=_param_cb)
def check(value):
app = click.get_current_context().obj.load_app()
app_obj = click.get_current_context().obj.load_app()
# the loaded app should be the same as current_app
same_app = current_app._get_current_object() is app
same_app = current_app._get_current_object() is app_obj
return same_app, value
cli = FlaskGroup(create_app=lambda: app)
@ -304,7 +304,7 @@ def test_app_cli_has_app_context(app, runner):
assert result.return_value == (True, True)
def test_with_appcontext(runner):
def test_with_appcontext(runner_):
@click.command()
@with_appcontext
def testcmd():
@ -312,12 +312,12 @@ def test_with_appcontext(runner):
obj = ScriptInfo(create_app=lambda: Flask("testapp"))
result = runner.invoke(testcmd, obj=obj)
result = runner_.invoke(testcmd, obj=obj)
assert result.exit_code == 0
assert result.output == "testapp\n"
def test_appgroup_app_context(runner):
def test_appgroup_app_context(cli_runner):
@click.group(cls=AppGroup)
def cli():
pass
@ -336,16 +336,16 @@ def test_appgroup_app_context(runner):
obj = ScriptInfo(create_app=lambda: Flask("testappgroup"))
result = runner.invoke(cli, ["test"], obj=obj)
result = cli_runner.invoke(cli, ["test"], obj=obj)
assert result.exit_code == 0
assert result.output == "testappgroup\n"
result = runner.invoke(cli, ["subgroup", "test2"], obj=obj)
result = cli_runner.invoke(cli, ["subgroup", "test2"], obj=obj)
assert result.exit_code == 0
assert result.output == "testappgroup\n"
def test_flaskgroup_app_context(runner):
def test_flaskgroup_app_context(runner_):
def create_app():
return Flask("flaskgroup")
@ -357,7 +357,7 @@ def test_flaskgroup_app_context(runner):
def test():
click.echo(current_app.name)
result = runner.invoke(cli, ["test"])
result = runner_.invoke(cli, ["test"])
assert result.exit_code == 0
assert result.output == "flaskgroup\n"
@ -370,14 +370,14 @@ def test_flaskgroup_debug(runner, set_debug_flag):
return app
@click.group(cls=FlaskGroup, create_app=create_app, set_debug_flag=set_debug_flag)
def cli(**params):
def cli_command(**params):
pass
@cli.command()
@cli_command.command()
def test():
click.echo(str(current_app.debug))
result = runner.invoke(cli, ["test"])
result = runner.invoke(cli_command, ["test"])
assert result.exit_code == 0
assert result.output == f"{not set_debug_flag}\n"
@ -388,18 +388,18 @@ def test_flaskgroup_nested(app, runner):
cli.add_command(flask_group)
@flask_group.command()
def show():
def show_command():
click.echo(current_app.name)
result = runner.invoke(cli, ["flask", "show"])
result = runner.invoke(cli, ["flask", "show_command"])
assert result.output == "flask_test\n"
def test_no_command_echo_loading_error():
from flask.cli import cli
runner = CliRunner(mix_stderr=False)
result = runner.invoke(cli, ["missing"])
cli_runner = CliRunner(mix_stderr=False)
result = cli_runner.invoke(cli, ["missing"])
assert result.exit_code == 2
assert "FLASK_APP" in result.stderr
assert "Usage:" in result.stderr
@ -408,8 +408,8 @@ def test_no_command_echo_loading_error():
def test_help_echo_loading_error():
from flask.cli import cli
runner = CliRunner(mix_stderr=False)
result = runner.invoke(cli, ["--help"])
cli_runner = CliRunner(mix_stderr=False)
result = cli_runner.invoke(cli, ["--help"])
assert result.exit_code == 0
assert "FLASK_APP" in result.stderr
assert "Usage:" in result.stdout
@ -420,8 +420,8 @@ def test_help_echo_exception():
raise Exception("oh no")
cli = FlaskGroup(create_app=create_app)
runner = CliRunner(mix_stderr=False)
result = runner.invoke(cli, ["--help"])
runner_obj = CliRunner(mix_stderr=False)
result = runner_obj.invoke(cli, ["--help"])
assert result.exit_code == 0
assert "Exception: oh no" in result.stderr
assert "Usage:" in result.stdout
@ -476,30 +476,30 @@ class TestRoutes:
output = invoke(["routes", "--all-methods"]).output
assert "GET, HEAD, OPTIONS, POST" in output
def test_no_routes(self, runner):
def test_no_routes(self, runner_):
app = Flask(__name__, static_folder=None)
cli = FlaskGroup(create_app=lambda: app)
result = runner.invoke(cli, ["routes"])
result = runner_.invoke(cli, ["routes"])
assert result.exit_code == 0
assert "No routes were registered." in result.output
def test_subdomain(self, runner):
app = Flask(__name__, static_folder=None)
app.add_url_rule("/a", subdomain="a", endpoint="a")
app.add_url_rule("/b", subdomain="b", endpoint="b")
cli = FlaskGroup(create_app=lambda: app)
result = runner.invoke(cli, ["routes"])
assert result.exit_code == 0
assert "Subdomain" in result.output
def test_subdomain(self, runner_):
app = Flask(__name__, static_folder=None)
app.add_url_rule("/a", subdomain="a", endpoint="a")
app.add_url_rule("/b", subdomain="b", endpoint="b")
cli = FlaskGroup(create_app=lambda: app)
result = runner_.invoke(cli, ["routes"])
assert result.exit_code == 0
assert "Subdomain" in result.output
def test_host(self, runner):
app = Flask(__name__, static_folder=None, host_matching=True)
app.add_url_rule("/a", host="a", endpoint="a")
app.add_url_rule("/b", host="b", endpoint="b")
cli = FlaskGroup(create_app=lambda: app)
result = runner.invoke(cli, ["routes"])
assert result.exit_code == 0
assert "Host" in result.output
def test_host(self, runner_):
app = Flask(__name__, static_folder=None, host_matching=True)
app.add_url_rule("/a", host="a", endpoint="a")
app.add_url_rule("/b", host="b", endpoint="b")
cli = FlaskGroup(create_app=lambda: app)
result = runner_.invoke(cli, ["routes"])
assert result.exit_code == 0
assert "Host" in result.output
def dotenv_not_available():
@ -558,10 +558,10 @@ def test_dotenv_optional(monkeypatch):
@need_dotenv
def test_disable_dotenv_from_env(monkeypatch, runner):
def test_disable_dotenv_from_env(monkeypatch, runner_):
monkeypatch.chdir(test_path)
monkeypatch.setitem(os.environ, "FLASK_SKIP_DOTENV", "1")
runner.invoke(FlaskGroup())
runner_.invoke(FlaskGroup())
assert "FOO" not in os.environ

View file

@ -196,8 +196,8 @@ def test_json_decimal():
def test_json_attr(app, client):
@app.route("/add", methods=["POST"])
def add():
json = flask.request.get_json()
return str(json["a"] + json["b"])
data = flask.request.get_json()
return str(data["a"] + data["b"])
rv = client.post(
"/add",