Skip to content

Commit

Permalink
Add lots of random fixes (#19)
Browse files Browse the repository at this point in the history
* Add lots of random fixes

* Fix python 3.8 compatibility

* Add more errors to ruff
  • Loading branch information
odarbelaeze authored Mar 18, 2024
1 parent b3c4c48 commit d7c82b1
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 57 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ jobs:
pip install setuptools wheel hatch
hatch env create
- name: Make sure we didn't forget anything in pre-commit
run: |
hatch run pre-commit run --all
- name: Test with pytest
run: |
hatch run test:run
hatch run pytest
- name: Build and publish
env:
Expand Down
23 changes: 10 additions & 13 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@ repos:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
language_version: python3
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
name: isort (python)
args: [ "--profile", "black" ]
- repo: https://github.com/charliermarsh/ruff-pre-commit
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: 'v0.0.254'
rev: v0.3.3
hooks:
# Run the linter.
- id: ruff
args: [ --fix ]
# Run the formatter.
- id: ruff-format
- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v1.9.0'
hooks:
- id: mypy
29 changes: 11 additions & 18 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,27 @@ dependencies = [
"bibtexparser~=1.4.0",
"networkx~=3.0",
"typer[all]~=0.9.0",
"xlsxwriter~=3.2.0",
]

[project.optional-dependencies]
dev = [
"black~=22.10.0",
"pytest~=7.2.0",
"isort~=5.12.0",
"pre-commit~=2.20.0",
"ruff~=0.0.254",
"ruff~=0.3.3",
"mypy~=1.9.0",
]

[tool.ruff]
[project.scripts]
bibx = "bibx.cli:app"

[tool.ruff.lint]
select = ["I", "E", "F", "W"]
ignore = ["E501"]

[tool.mypy]
mypy_path = "./stubs/"

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
Expand All @@ -40,17 +47,3 @@ path = "src/bibx/__init__.py"

[tool.hatch.envs.default]
features = ["dev"]

[tool.hatch.envs.test]
dependencies = [
"coverage[toml]",
"pytest",
"pytest-cov",
]
[tool.hatch.envs.test.scripts]
run-coverage = "pytest --cov-config=pyproject.toml --cov=bibx --cov=tests"
run = "run-coverage --no-cov"

[tool.hatch.envs.docs.scripts]
build = "mkdocs build --clean --strict"
serve = "mkdocs serve --dev-addr localhost:8000"
2 changes: 1 addition & 1 deletion src/bibx/_entities/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def cited_by_year(self) -> Dict[int, int]:
cited_items_per_year[year] = 0

for article in self.articles:
if article.times_cited is None:
if article.times_cited is None or article.year is None:
continue
if article.year in cited_items_per_year:
cited_items_per_year[article.year] += article.times_cited
Expand Down
3 changes: 1 addition & 2 deletions src/bibx/_entities/collection_builders/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@


class CollectionBuilder(Protocol):
def build(self) -> Collection:
...
def build(self) -> Collection: ...
8 changes: 5 additions & 3 deletions src/bibx/_entities/collection_builders/scopus_bib.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ def _article_from_entry(self, entry: dict) -> Article:
if "author" not in entry or "year" not in entry:
raise MissingCriticalInformation()
if "note" in entry:
times_cited = re.search(r"cited By (\d+)", entry["note"], re.IGNORECASE)
if times_cited:
times_cited = int(times_cited.groups()[0])
match = re.search(r"cited By (\d+)", entry["note"], re.IGNORECASE)
if match:
times_cited = int(match.groups()[0])
else:
times_cited = None
else:
times_cited = None
return Article(
Expand Down
10 changes: 6 additions & 4 deletions src/bibx/_entities/collection_builders/scopus_ris.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def _size(file) -> int:
return size


def _int_or_nothing(raw: List[str]) -> Optional[int]:
def _int_or_nothing(raw: Optional[List[str]]) -> Optional[int]:
if not raw:
return None
try:
Expand Down Expand Up @@ -102,9 +102,11 @@ def _article_form_reference(cls, scopusref: str) -> Article:
return Article(
authors=[f"{first_name} {last_name.replace(' ', '').replace('.', '')}"],
year=int(year),
journal=journal.strip().replace(".", "").upper()
if not journal.isspace()
else None,
journal=(
journal.strip().replace(".", "").upper()
if not journal.isspace()
else None
),
volume=volume_info.get("volume"),
page=volume_info.get("page"),
doi=doi,
Expand Down
2 changes: 1 addition & 1 deletion src/bibx/_entities/collection_builders/wos.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def _get_articles_from_references(

@classmethod
def _parse_article_from_str(cls, article_as_str: str) -> Article:
article_data = collections.defaultdict(list)
article_data: Dict[str, List[str]] = collections.defaultdict(list)
article_data.setdefault("CR", [])
field = None
for line in article_as_str.split("\n"):
Expand Down
4 changes: 2 additions & 2 deletions src/bibx/algorithms/sap.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def clean_graph(g: nx.DiGraph) -> nx.DiGraph:
"""
# Extract the giant component of the graph
giant_component_nodes = max(nx.weakly_connected_components(g), key=len)
giant: nx.DiGraph = g.subgraph(giant_component_nodes).copy()
giant: nx.DiGraph = cast(nx.DiGraph, g.subgraph(giant_component_nodes).copy())

# Remove nodes that cite one element and are never cited themselves
giant.remove_nodes_from(
Expand Down Expand Up @@ -312,4 +312,4 @@ def _clear(graph: nx.DiGraph) -> nx.DiGraph:
and graph.nodes[n][TRUNK] > 0
and graph.nodes[n][LEAF] > 0
]
return graph.subgraph(nodes)
return cast(nx.DiGraph, graph.subgraph(nodes))
3 changes: 3 additions & 0 deletions stubs/bibtexparser/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from _typeshed import Incomplete

def load(bibtex_file, parser: Incomplete | None = None): ...
31 changes: 31 additions & 0 deletions stubs/networkx/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import Any, Dict, Iterable, Iterator, List

from _typeshed import Incomplete, Self

__version__: str

class Graph:
def subgraph(self: Self, nodes: Iterable) -> Self: ...
def copy(self: Self) -> Self: ...
@property
def nodes(self) -> Dict[str, Incomplete]: ...
def add_node(self, node: str, **kwargs) -> None: ...
def add_edge(self, u: str, v: str, **kwargs) -> None: ...
def add_edges_from(self, edges: Iterable) -> None: ...
def remove_nodes_from(self, edges: Iterable) -> None: ...
def remove_edges_from(self, edges: Iterable) -> None: ...
def __iter__(self) -> Iterator[str]: ...
def in_degree(self, node: str) -> int: ...
def out_degree(self, node: str) -> int: ...

class DiGraph(Graph):
def to_undirected(self) -> Graph: ...
def successors(self, node: str) -> Iterable[str]: ...
def predecessors(self, node: str) -> Iterable[str]: ...

# NOTE: The attr parameter of this function is quite more complicated than this
def set_node_attributes(g: DiGraph, attr: Any, name: str) -> None: ...
def topological_sort(g: DiGraph) -> Iterable[str]: ...
def selfloop_edges(g: DiGraph) -> Iterable[str]: ...
def weakly_connected_components(g: DiGraph) -> Iterable[List[str]]: ...
def strongly_connected_components(g: DiGraph) -> Iterable[List[str]]: ...
3 changes: 3 additions & 0 deletions stubs/networkx/algorithms/community/louvain.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from networkx import Graph

def louvain_communities(g: Graph): ...
1 change: 0 additions & 1 deletion tests/algorithms/test_sap.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import networkx as nx

from bibx.algorithms.sap import Sap


Expand Down
22 changes: 11 additions & 11 deletions tests/entities/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
times_cited=0,
references=[],
keywords=[],
sources=[],
sources=set(),
extra={},
_label=None,
),
Expand All @@ -28,7 +28,7 @@
times_cited=2,
references=[],
keywords=[],
sources=[],
sources=set(),
extra={},
_label=None,
),
Expand All @@ -43,7 +43,7 @@
times_cited=12,
references=[],
keywords=[],
sources=[],
sources=set(),
extra={},
_label=None,
),
Expand All @@ -58,7 +58,7 @@
times_cited=2,
references=[],
keywords=[],
sources=[],
sources=set(),
extra={},
_label=None,
),
Expand All @@ -73,7 +73,7 @@
times_cited=0,
references=[],
keywords=[],
sources=[],
sources=set(),
extra={},
_label=None,
),
Expand All @@ -88,7 +88,7 @@
times_cited=None,
references=[],
keywords=[],
sources=[],
sources=set(),
extra={},
_label=None,
),
Expand All @@ -103,7 +103,7 @@
times_cited=1,
references=[],
keywords=[],
sources=[],
sources=set(),
extra={},
_label=None,
),
Expand All @@ -118,7 +118,7 @@
times_cited=20,
references=[],
keywords=[],
sources=[],
sources=set(),
extra={},
_label=None,
),
Expand All @@ -132,7 +132,7 @@
doi="19",
references=[],
keywords=[],
sources=[],
sources=set(),
extra={},
_label=None,
),
Expand All @@ -146,7 +146,7 @@
doi="19",
references=[],
keywords=[],
sources=[],
sources=set(),
extra={},
_label=None,
),
Expand All @@ -159,7 +159,7 @@
doi="19",
references=[],
keywords=[],
sources=[],
sources=set(),
extra={},
_label=None,
),
Expand Down

0 comments on commit d7c82b1

Please sign in to comment.