Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions mypy_primer/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Project:

mypy_cmd: str
pyright_cmd: str | None
paths: list[str] | None = None

install_cmd: str | None = None
deps: list[str] | None = None
Expand Down Expand Up @@ -55,6 +56,8 @@ def __repr__(self) -> str:
result += f", name_override={self.name_override!r}"
if self.pyright_cmd:
result += f", pyright_cmd={self.pyright_cmd!r}"
if self.paths:
result += f", paths={self.paths!r}"
if self.install_cmd:
result += f", install_cmd={self.install_cmd!r}"
if self.deps:
Expand Down Expand Up @@ -168,7 +171,7 @@ async def setup(self) -> None:
def get_mypy_cmd(self, mypy: str | Path, additional_flags: Sequence[str] = ()) -> str:
mypy_cmd = self.mypy_cmd
assert "{mypy}" in self.mypy_cmd
mypy_cmd = mypy_cmd.format(mypy=mypy)
mypy_cmd = mypy_cmd.format_map(_FormatMap(mypy=mypy, paths=self.paths))

python_exe = self.venv.python
mypy_cmd += f" --python-executable={quote_path(python_exe)}"
Expand Down Expand Up @@ -250,7 +253,11 @@ def get_pyright_cmd(self, pyright: Path, additional_flags: Sequence[str] = ()) -
assert "{pyright}" in pyright_cmd
if additional_flags:
pyright_cmd += " " + " ".join(additional_flags)
pyright_cmd = pyright_cmd.format(pyright=f"node {pyright}")

pyright_cmd = pyright_cmd.format_map(
_FormatMap(pyright=f"node {pyright}", paths=self.paths)
)

return pyright_cmd

async def run_pyright(
Expand Down Expand Up @@ -345,6 +352,21 @@ def from_location(cls, location: str) -> Project:
)


class _FormatMap:
def __init__(self, **map: str | Path | list[str] | None) -> None:
self.map = map

def __getitem__(self, key: str) -> str | Path:
if key not in self.map:
raise KeyError(key)
value = self.map[key]
if value is None:
raise ValueError(f"Required {key} to be specified")
if isinstance(value, list):
value = " ".join(value)
return value


@dataclass(frozen=True)
class TypeCheckResult:
command: str
Expand Down
Loading