Skip to content

Commit

Permalink
sty: pacify flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
oesteban committed Mar 15, 2024
1 parent e53e988 commit 2e783d2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 39 deletions.
19 changes: 10 additions & 9 deletions .maint/update_requirements.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
from copy import copy
from pathlib import Path

from packaging.requirements import Requirement, SpecifierSet

try:
Expand All @@ -9,13 +10,13 @@
from pip._vendor.tomli import loads

repo_root = Path(__file__).parent.parent
pyproject = repo_root / "pyproject.toml"
reqs = repo_root / "requirements.txt"
min_reqs = repo_root / "min-requirements.txt"
pyproject = repo_root / 'pyproject.toml'
reqs = repo_root / 'requirements.txt'
min_reqs = repo_root / 'min-requirements.txt'

requirements = [
Requirement(req)
for req in loads(pyproject.read_text())["project"]["dependencies"]
for req in loads(pyproject.read_text())['project']['dependencies']
]

script_name = Path(__file__).relative_to(repo_root)
Expand All @@ -25,20 +26,20 @@ def to_min(req):
if req.specifier:
req = copy(req)
try:
min_spec = [spec for spec in req.specifier if spec.operator in (">=", "~=")][0]
min_spec = [spec for spec in req.specifier if spec.operator in ('>=', '~=')][0]
except IndexError:
return req
min_spec._spec = ("==",) + min_spec._spec[1:]
min_spec._spec = ('==',) + min_spec._spec[1:]
req.specifier = SpecifierSet(str(min_spec))
return req


lines = [f"# Auto-generated by {script_name}", ""]
lines = [f'# Auto-generated by {script_name}', '']

# Write requirements
lines[1:-1] = [str(req) for req in requirements]
reqs.write_text("\n".join(lines))
reqs.write_text('\n'.join(lines))

# Write minimum requirements
lines[1:-1] = [str(to_min(req)) for req in requirements]
min_reqs.write_text("\n".join(lines))
min_reqs.write_text('\n'.join(lines))
2 changes: 1 addition & 1 deletion templateflow/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""TemplateFlow's Python Client."""
import sys
from importlib import import_module
from json import loads
from pathlib import Path
from bids.layout import Query
Expand All @@ -11,6 +10,7 @@
item for item in dir(TF_LAYOUT) if item.startswith("get_")
)


@requires_layout
def ls(template, **kwargs):
"""
Expand Down
59 changes: 31 additions & 28 deletions templateflow/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(self, bibtex):
self.pairs = {}

# DOI could not be converted
if self.text.startswith("http"):
if self.text.startswith('http'):
self.url_only = True
else:
self._parse_bibtex()
Expand All @@ -23,15 +23,15 @@ def _parse_bibtex(self):
import re

try:
self.etype = re.search(r"@(\w+)", self.text).group(1)
self.etype = re.search(r'@(\w+)', self.text).group(1)
except AttributeError:
raise TypeError(f"Invalid bibtex: {self.text}")
raise TypeError(f'Invalid bibtex: {self.text}')
try:
self.citekey = re.search(r"@[^{]*{([^,\s]+)", self.text).group(1)
self.citekey = re.search(r'@[^{]*{([^,\s]+)', self.text).group(1)
except AttributeError:
raise TypeError(f"Invalid bibtex: {self.text}")
raise TypeError(f'Invalid bibtex: {self.text}')
self.pairs = {
key: val for key, val in re.findall(r"(\w+)=(\{[^{}]+\})", self.text)
key: val for key, val in re.findall(r'(\w+)=(\{[^{}]+\})', self.text)
}

def get(self, val):
Expand All @@ -41,7 +41,10 @@ def __str__(self):
return self.text

def __repr__(self):
return f'@{self.etype}{{{self.citekey}, {", ".join([f"{key} = {val}" for key, val in self.pairs.items()])}}}'
return (
f'@{self.etype}{{{self.citekey}, '
f'{", ".join([f"{key} = {val}" for key, val in self.pairs.items()])}}}'
)

def __eq__(self, other):
if isinstance(other, Bibtex):
Expand All @@ -58,26 +61,26 @@ def __eq__(self, other):
def assert_same(self, other):
"""Convenience method to find deviations between two Bibtex objects"""
assert isinstance(other, Bibtex)
assert self.etype == other.etype, "Mismatched entry types"
assert self.citekey == other.citekey, "Mismatched citekeys"
assert self.etype == other.etype, 'Mismatched entry types'
assert self.citekey == other.citekey, 'Mismatched citekeys'
for key in self.pairs.keys():
assert key in other.pairs, f"Key ({key}) missing from other"
assert key in other.pairs, f'Key ({key}) missing from other'
assert (
self.pairs[key] == other.pairs[key]
), f"Key ({key}) mismatched\n\n{self.pairs[key]}\n\n{other.pairs[key]}"
), f'Key ({key}) mismatched\n\n{self.pairs[key]}\n\n{other.pairs[key]}'

for key in other.pairs.keys():
assert key in self.pairs, f"Key ({key}) missing from pairs"
assert key in self.pairs, f'Key ({key}) missing from pairs'

assert self.pairs == other.pairs, "Dictionaries do not match"
assert self.pairs == other.pairs, 'Dictionaries do not match'


# test setup to avoid cluttering pytest parameterize
mni2009_urls = [
"https://doi.org/10.1016/j.neuroimage.2010.07.033",
"https://doi.org/10.1016/S1053-8119(09)70884-5",
"http://nist.mni.mcgill.ca/?p=904",
"https://doi.org/10.1007/3-540-48714-X_16",
'https://doi.org/10.1016/j.neuroimage.2010.07.033',
'https://doi.org/10.1016/S1053-8119(09)70884-5',
'http://nist.mni.mcgill.ca/?p=904',
'https://doi.org/10.1007/3-540-48714-X_16',
]

mni2009_fbib = """\
Expand Down Expand Up @@ -111,8 +114,8 @@ def assert_same(self, other):
}"""

fslr_urls = [
"https://doi.org/10.1093/cercor/bhr291",
"https://github.com/Washington-University/HCPpipelines/tree/master/global/templates",
'https://doi.org/10.1093/cercor/bhr291',
'https://github.com/Washington-University/HCPpipelines/tree/master/global/templates',
]

fslr_fbib = """\
Expand All @@ -132,7 +135,7 @@ def assert_same(self, other):
}"""

fslr_lbib = (
"https://github.com/Washington-University/HCPpipelines/tree/master/global/templates"
'https://github.com/Washington-University/HCPpipelines/tree/master/global/templates'
)

fsaverage_fbib = """\
Expand All @@ -152,14 +155,14 @@ def assert_same(self, other):


@pytest.mark.parametrize(
"template,urls,fbib,lbib",
'template,urls,fbib,lbib',
[
("MNI152NLin2009cAsym", mni2009_urls, mni2009_fbib, mni2009_lbib),
("fsLR", fslr_urls, fslr_fbib, fslr_lbib),
('MNI152NLin2009cAsym', mni2009_urls, mni2009_fbib, mni2009_lbib),
('fsLR', fslr_urls, fslr_fbib, fslr_lbib),
(
"fsaverage",
'fsaverage',
[
"https://doi.org/10.1002/(sici)1097-0193(1999)8:4%3C272::aid-hbm10%3E3.0.co;2-4"
'https://doi.org/10.1002/(sici)1097-0193(1999)8:4%3C272::aid-hbm10%3E3.0.co;2-4'
],
fsaverage_fbib,
None,
Expand Down Expand Up @@ -189,12 +192,12 @@ def test_citations(tmp_path, template, urls, fbib, lbib):
def test_pybids_magic_get():
"""Check automatic entity expansion of the layout."""
assert sorted(api.ls_atlases()) == sorted(api.TF_LAYOUT.get_atlases())
assert sorted(api.ls_atlases(template="MNI152NLin6ASym")) == sorted(
api.TF_LAYOUT.get_atlases(template="MNI152NLin6ASym")
assert sorted(api.ls_atlases(template='MNI152NLin6ASym')) == sorted(
api.TF_LAYOUT.get_atlases(template='MNI152NLin6ASym')
)

with pytest.raises(TypeError):
api.ls_atlases("MNI152NLin6ASym")
api.ls_atlases('MNI152NLin6ASym')

# Existing layout.get_* should not be bubbled to the layout
# (that means, raise an AttributeError instead of a BIDSEntityError)
Expand Down
1 change: 0 additions & 1 deletion templateflow/tests/test_version.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Test _version.py."""
import sys
from collections import namedtuple
from importlib.metadata import PackageNotFoundError
from importlib import reload
import templateflow
Expand Down

0 comments on commit 2e783d2

Please sign in to comment.