Skip to content

Commit

Permalink
Merge pull request #2214 from NovakApis/feature/python-typing/list.py
Browse files Browse the repository at this point in the history
Feature/python typing/list.py
  • Loading branch information
kedhammar authored Oct 18, 2023
2 parents 8d0e1f3 + c699bb5 commit 3f3486b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint-code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,4 @@ jobs:
**/*.py
- name: Run if any of the listed files above is changed
if: steps.changed-py-files.outputs.any_changed == 'true'
run: mypy ${{ steps.changed-py-files.outputs.all_changed_files }} --ignore-missing-imports
run: mypy ${{ steps.changed-py-files.outputs.all_changed_files }}
30 changes: 21 additions & 9 deletions nf_core/components/list.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import logging
from typing import Dict, List, Optional, Tuple, Union, cast

import rich

Expand All @@ -11,11 +12,19 @@


class ComponentList(ComponentCommand):
def __init__(self, component_type, pipeline_dir, remote=True, remote_url=None, branch=None, no_pull=False):
def __init__(
self,
component_type: str,
pipeline_dir: str,
remote: bool = True,
remote_url: Optional[str] = None,
branch: Optional[str] = None,
no_pull: bool = False,
) -> None:
super().__init__(component_type, pipeline_dir, remote_url, branch, no_pull)
self.remote = remote

def list_components(self, keywords=None, print_json=False):
def list_components(self, keywords: Optional[List[str]] = None, print_json=False) -> rich.table.Table:
keywords = keywords or []
"""
Get available modules/subworkflows names from GitHub tree for repo
Expand All @@ -25,11 +34,11 @@ def list_components(self, keywords=None, print_json=False):
# self.check_component_structure(self.component_type)

# Initialise rich table
table = rich.table.Table()
table: rich.table.Table = rich.table.Table()
table.add_column(f"{self.component_type[:-1].capitalize()} Name")
components = []
components: List[str] = []

def pattern_msg(keywords):
def pattern_msg(keywords: List[str]):
if len(keywords) == 0:
return ""
if len(keywords) == 1:
Expand Down Expand Up @@ -78,11 +87,11 @@ def pattern_msg(keywords):
return ""

# Verify that 'modules.json' is consistent with the installed modules
modules_json = ModulesJson(self.dir)
modules_json: ModulesJson = ModulesJson(self.dir)
modules_json.check_up_to_date()

# Filter by keywords
repos_with_comps = {
repos_with_comps: Dict[str, List[Tuple[str, str]]] = {
repo_url: [comp for comp in components if all(k in comp[1] for k in keywords)]
for repo_url, components in modules_json.get_all_components(self.component_type).items()
}
Expand All @@ -101,10 +110,13 @@ def pattern_msg(keywords):
modules_json = modules_json.modules_json

for repo_url, component_with_dir in sorted(repos_with_comps.items()):
repo_entry: Dict[str, Dict[str, Dict[str, Dict[str, Union[str, List[str]]]]]]

repo_entry = modules_json["repos"].get(repo_url, {})
for install_dir, component in sorted(component_with_dir):
repo_modules = repo_entry.get(self.component_type)
component_entry = repo_modules.get(install_dir).get(component)
# Use cast() to predict the return type of recursive get():s
repo_modules = cast(dict, repo_entry.get(self.component_type))
component_entry = cast(dict, cast(dict, repo_modules.get(install_dir)).get(component))

if component_entry:
version_sha = component_entry["git_sha"]
Expand Down
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ pytest-datafiles
responses
Sphinx
sphinx-rtd-theme
mypy
types-PyYAML

0 comments on commit 3f3486b

Please sign in to comment.