Modernize code to Python 3.6+ (#1158)

* Compact dict and set building
* Remove redundant parentheses
* Simplify chained conditions
* Change method name to lowercase
* Use triple double quotes for docstrings

Thanks, @eumiro!
This commit is contained in:
Miroslav Šedivý 2020-12-23 18:04:32 +01:00 committed by GitHub
commit a882d67962
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 76 additions and 82 deletions

View file

@ -751,7 +751,7 @@ def assert_permissions_checked(datasette, actions):
help="Delete and recreate database if it exists",
)
def cli(db_filename, metadata, plugins_path, recreate):
"Write out the fixtures database used by Datasette's test suite"
"""Write out the fixtures database used by Datasette's test suite"""
if metadata and not metadata.endswith(".json"):
raise click.ClickException("Metadata should end with .json")
if not db_filename.endswith(".db"):

View file

@ -12,7 +12,7 @@ ureg = pint.UnitRegistry()
@hookimpl
def prepare_connection(conn, database, datasette):
def convert_units(amount, from_, to_):
"select convert_units(100, 'm', 'ft');"
"""select convert_units(100, 'm', 'ft');"""
return (amount * ureg(from_)).to(to_).to_tuple()[0]
conn.create_function("convert_units", 3, convert_units)

View file

@ -789,7 +789,7 @@ def test_table_shape_object(app_client):
} == response.json
def test_table_shape_object_compound_primary_Key(app_client):
def test_table_shape_object_compound_primary_key(app_client):
response = app_client.get("/fixtures/compound_primary_key.json?_shape=object")
assert {"a,b": {"pk1": "a", "pk2": "b", "content": "c"}} == response.json
@ -871,7 +871,7 @@ def test_validate_page_size(app_client, path, expected_error):
def test_page_size_zero(app_client):
"For _size=0 we return the counts, empty rows and no continuation token"
"""For _size=0 we return the counts, empty rows and no continuation token"""
response = app_client.get("/fixtures/no_primary_key.json?_size=0")
assert 200 == response.status
assert [] == response.json["rows"]

View file

@ -5,7 +5,7 @@ import time
def test_auth_token(app_client):
"The /-/auth-token endpoint sets the correct cookie"
"""The /-/auth-token endpoint sets the correct cookie"""
assert app_client.ds._root_token is not None
path = f"/-/auth-token?token={app_client.ds._root_token}"
response = app_client.get(
@ -29,7 +29,7 @@ def test_auth_token(app_client):
def test_actor_cookie(app_client):
"A valid actor cookie sets request.scope['actor']"
"""A valid actor cookie sets request.scope['actor']"""
cookie = app_client.actor_cookie({"id": "test"})
response = app_client.get("/", cookies={"ds_actor": cookie})
assert {"id": "test"} == app_client.ds._last_request.scope["actor"]

View file

@ -110,7 +110,7 @@ def test_plugins_cli(app_client):
result2 = runner.invoke(cli, ["plugins", "--all"])
names = [p["name"] for p in json.loads(result2.output)]
# Should have all the EXPECTED_PLUGINS
assert set(names).issuperset(set(p["name"] for p in EXPECTED_PLUGINS))
assert set(names).issuperset({p["name"] for p in EXPECTED_PLUGINS})
# And the following too:
assert set(names).issuperset(DEFAULT_PLUGINS)

View file

@ -15,7 +15,7 @@ label_re = re.compile(r"\.\. _([^\s:]+):")
def get_headings(content, underline="-"):
heading_re = re.compile(r"(\w+)(\([^)]*\))?\n\{}+\n".format(underline))
return set(h[0] for h in heading_re.findall(content))
return {h[0] for h in heading_re.findall(content)}
def get_labels(filename):
@ -96,11 +96,11 @@ def documented_table_filters():
json_api_rst = (docs_path / "json_api.rst").read_text()
section = json_api_rst.split(".. _table_arguments:")[-1]
# Lines starting with ``?column__exact= are docs for filters
return set(
return {
line.split("__")[1].split("=")[0]
for line in section.split("\n")
if line.startswith("``?column__")
)
}
@pytest.mark.parametrize("filter", [f.key for f in Filters._filters])

View file

@ -437,7 +437,7 @@ def cascade_app_client():
],
)
def test_permissions_cascade(cascade_app_client, path, permissions, expected_status):
"Test that e.g. having view-table but NOT view-database lets you view table page, etc"
"""Test that e.g. having view-table but NOT view-database lets you view table page, etc"""
allow = {"id": "*"}
deny = {}
previous_metadata = cascade_app_client.ds._metadata

View file

@ -28,7 +28,7 @@ at_memory_re = re.compile(r" at 0x\w+")
"plugin_hook", [name for name in dir(pm.hook) if not name.startswith("_")]
)
def test_plugin_hooks_have_tests(plugin_hook):
"Every plugin hook should be referenced in this test module"
"""Every plugin hook should be referenced in this test module"""
tests_in_this_module = [t for t in globals().keys() if t.startswith("test_hook_")]
ok = False
for test in tests_in_this_module: