Skip to content

Commit 7e88ff6

Browse files
committed
Fix pyrefly errors
1 parent 7a57b65 commit 7e88ff6

File tree

3 files changed

+45
-42
lines changed

3 files changed

+45
-42
lines changed

kicad_lib_manager/config.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -274,16 +274,9 @@ def _normalize_libraries_field(self) -> None:
274274
# Validate and clean up each library entry
275275
normalized_libraries: List[LibraryDict] = []
276276
for lib in libraries:
277-
if isinstance(lib, dict) and all(
278-
key in lib for key in ["name", "path", "type"]
279-
):
280-
# Ensure all values are strings
281-
normalized_lib = _make_library_dict(
282-
name=str(lib["name"]),
283-
path=str(lib["path"]),
284-
type_=str(lib["type"]),
285-
)
286-
normalized_libraries.append(normalized_lib)
277+
validated_lib = _validate_library_entry(lib)
278+
if validated_lib is not None:
279+
normalized_libraries.append(validated_lib)
287280
else:
288281
click.echo(f"Warning: Skipping invalid library entry: {lib}", err=True)
289282

@@ -312,16 +305,9 @@ def _get_normalized_libraries(self) -> List[LibraryDict]:
312305
needs_save = False
313306

314307
for lib in libraries_raw:
315-
if isinstance(lib, dict) and all(
316-
key in lib for key in ["name", "path", "type"]
317-
):
318-
# Ensure all values are strings
319-
normalized_lib = _make_library_dict(
320-
name=str(lib["name"]),
321-
path=str(lib["path"]),
322-
type_=str(lib["type"]),
323-
)
324-
normalized_libraries.append(normalized_lib)
308+
validated_lib = _validate_library_entry(lib)
309+
if validated_lib is not None:
310+
normalized_libraries.append(validated_lib)
325311
else:
326312
click.echo(f"Warning: Skipping invalid library entry: {lib}", err=True)
327313
needs_save = True
@@ -336,4 +322,17 @@ def _get_normalized_libraries(self) -> List[LibraryDict]:
336322

337323
def _make_library_dict(name: str, path: str, type_: str) -> LibraryDict:
338324
"""Typed constructor for `LibraryDict` to satisfy type checker."""
339-
return cast("LibraryDict", {"name": name, "path": path, "type": type_})
325+
return LibraryDict(name=name, path=path, type=type_)
326+
327+
328+
def _validate_library_entry(
329+
lib: Union[Dict[str, str], LibraryDict],
330+
) -> Optional[LibraryDict]:
331+
"""Validate and normalize a library entry."""
332+
if isinstance(lib, dict) and all(key in lib for key in ["name", "path", "type"]):
333+
return _make_library_dict(
334+
name=str(lib["name"]),
335+
path=str(lib["path"]),
336+
type_=str(lib["type"]),
337+
)
338+
return None

kicad_lib_manager/utils/template.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ def create_template_metadata(
276276
metadata["extends"] = extends
277277

278278
if dependencies:
279-
metadata["dependencies"] = {"recommended": dependencies}
279+
deps_dict: Dict[str, Any] = {"recommended": dependencies}
280+
metadata["dependencies"] = deps_dict
280281

281282
return metadata
282283

@@ -409,9 +410,9 @@ def create_template_structure(
409410
MAX_PATH_LENGTH = 512
410411

411412
# Track main KiCad files
412-
main_project_file = None
413-
main_schematic_file = None
414-
main_pcb_file = None
413+
main_project_file: Optional[str] = None
414+
main_schematic_file: Optional[str] = None
415+
main_pcb_file: Optional[str] = None
415416

416417
# First scan to find main KiCad files
417418
click.echo("Scanning for main KiCad files...")
@@ -430,31 +431,32 @@ def create_template_structure(
430431
continue
431432

432433
# Look for KiCad files in the root directory
433-
for file in files:
434-
rel_path = str(Path(rel_root) / file)
435-
git_path = rel_path.replace(os.sep, "/")
434+
for file_name in files:
435+
file_path = Path(rel_root) / file_name
436+
rel_path_str = str(file_path)
437+
git_path = rel_path_str.replace(os.sep, "/")
436438

437439
# Skip gitignored files
438-
if gitignore_spec and gitignore_spec.match_file(git_path):
440+
if gitignore_spec is not None and gitignore_spec.match_file(git_path):
439441
continue
440442

441443
# Look for main files in the root directory or top-level folders
442444
if rel_root == "." or len(Path(rel_root).parts) <= 1:
443-
file_lower = file.lower()
445+
file_lower = file_name.lower()
444446

445447
if file_lower.endswith(KICAD_PROJECT_EXT) and not main_project_file:
446-
main_project_file = str(Path(root) / file)
447-
click.echo(f"Found main project file: {file}")
448+
main_project_file = str(Path(root) / file_name)
449+
click.echo(f"Found main project file: {file_name}")
448450

449451
elif (
450452
file_lower.endswith(KICAD_SCHEMATIC_EXT) and not main_schematic_file
451453
):
452-
main_schematic_file = str(Path(root) / file)
453-
click.echo(f"Found main schematic file: {file}")
454+
main_schematic_file = str(Path(root) / file_name)
455+
click.echo(f"Found main schematic file: {file_name}")
454456

455457
elif file_lower.endswith(KICAD_PCB_EXT) and not main_pcb_file:
456-
main_pcb_file = str(Path(root) / file)
457-
click.echo(f"Found main PCB file: {file}")
458+
main_pcb_file = str(Path(root) / file_name)
459+
click.echo(f"Found main PCB file: {file_name}")
458460

459461
# Copy files from source to template
460462
for root, dirs, files in os.walk(source_directory):
@@ -476,7 +478,7 @@ def create_template_structure(
476478

477479
# Ensure proper path format for gitignore matching
478480
# (pathspec expects paths with forward slashes and trailing slash for directories)
479-
git_path = rel_path.replace(os.sep, "/")
481+
git_path = str(rel_path).replace(os.sep, "/")
480482
if not git_path.endswith("/"):
481483
git_path += "/"
482484

@@ -516,7 +518,7 @@ def create_template_structure(
516518
rel_path = Path(rel_root) / file
517519

518520
# Ensure proper path format for gitignore matching
519-
git_path = rel_path.replace(os.sep, "/")
521+
git_path = str(rel_path).replace(os.sep, "/")
520522

521523
# Skip gitignored files and additional excluded files
522524
if gitignore_spec and gitignore_spec.match_file(git_path):

tests/test_config_commands.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
Tests for KiCad Library Manager config commands.
33
"""
44

5+
from typing import List
6+
57
import pytest
68
from click.testing import CliRunner
79

810
from kicad_lib_manager.cli import main
9-
from kicad_lib_manager.config import Config
11+
from kicad_lib_manager.config import Config, LibraryDict
1012

1113
# Sample test data
12-
TEST_LIBRARIES = [
13-
{"name": "test-github-lib", "path": "/path/to/github/library", "type": "github"},
14-
{"name": "test-cloud-lib", "path": "/path/to/cloud/library", "type": "cloud"},
14+
TEST_LIBRARIES: List[LibraryDict] = [
15+
LibraryDict(name="test-github-lib", path="/path/to/github/library", type="github"),
16+
LibraryDict(name="test-cloud-lib", path="/path/to/cloud/library", type="cloud"),
1517
]
1618

1719

0 commit comments

Comments
 (0)