33import json
44import os
55import sys
6- from collections .abc import MutableMapping
6+ from collections .abc import Iterable
77from importlib .resources import files
88from pathlib import Path
9- from typing import Any
9+ from typing import TypedDict
10+
11+ from vcs_versioning ._overrides import ConfigOverridesDict
1012
1113from .. import _discover as discover
1214from .._config import Configuration
1517from ._args import CliNamespace , get_cli_parser
1618
1719
20+ class OutputData (TypedDict , ConfigOverridesDict , total = False ):
21+ version : str
22+ files : list [str ]
23+ queries : list [str ]
24+
25+
1826def _get_version_for_cli (config : Configuration , opts : CliNamespace ) -> str :
1927 """Get version string for CLI output, handling special cases and exceptions."""
2028 if opts .no_version :
@@ -68,7 +76,7 @@ def main(
6876
6977# flake8: noqa: C901
7078def command (opts : CliNamespace , version : str , config : Configuration ) -> int :
71- data : dict [ str , Any ] = {}
79+ data : OutputData = {}
7280
7381 if opts .command == "ls" :
7482 opts .query = ["files" ]
@@ -100,7 +108,7 @@ def command(opts: CliNamespace, version: str, config: Configuration) -> int:
100108 try :
101109 if q .startswith ("_" ):
102110 raise AttributeError ()
103- data [q ] = getattr (config , q )
111+ data [q ] = getattr (config , q ) # type: ignore[literal-required]
104112 except AttributeError :
105113 sys .stderr .write (f"Error: unknown query: '{ q } '\n " )
106114 return 1
@@ -110,11 +118,11 @@ def command(opts: CliNamespace, version: str, config: Configuration) -> int:
110118 return 0
111119
112120
113- def print_json (data : MutableMapping [ str , Any ] ) -> None :
121+ def print_json (data : OutputData ) -> None :
114122 print (json .dumps (data , indent = 2 ))
115123
116124
117- def print_plain (data : MutableMapping [ str , Any ] ) -> None :
125+ def print_plain (data : OutputData ) -> None :
118126 version = data .pop ("version" , None )
119127 if version :
120128 print (version )
@@ -125,15 +133,16 @@ def print_plain(data: MutableMapping[str, Any]) -> None:
125133 for query in queries :
126134 print (query )
127135 if data :
128- print ("\n " .join (data .values ()))
136+ print ("\n " .join (map ( str , data .values () )))
129137
130138
131- def print_key_value (data : MutableMapping [ str , Any ] ) -> None :
139+ def print_key_value (data : OutputData ) -> None :
132140 for key , value in data .items ():
133141 if isinstance (value , str ):
134142 print (f"{ key } = { value } " )
135143 else :
136- str_value = "\n " .join (value )
144+ assert isinstance (value , Iterable )
145+ str_value = "\n " .join (map (str , value ))
137146 print (f"{ key } = { str_value } " )
138147
139148
0 commit comments