1 # -----------------------------------------------------------------------------
2 # Copyright © 2009- The QtPy Contributors
4 # Released under the terms of the MIT License
5 # (see LICENSE.txt for details)
6 # -----------------------------------------------------------------------------
8 """Provide a CLI to allow configuring developer settings, including mypy."""
10 # Standard library imports
17 """Print the current version of the package."""
20 print("QtPy version", qtpy
.__version
__)
24 """Get the status of each Qt API usage."""
27 return {name
: name
== qtpy
.API
for name
in qtpy
.API_NAMES
}
30 def generate_mypy_args():
31 """Generate a string with always-true/false args to pass to mypy."""
32 options
= {False: "--always-false", True: "--always-true"}
34 apis_active
= get_api_status()
36 f
"{options[is_active]}={name.upper()}"
37 for name
, is_active
in apis_active
.items()
41 def generate_pyright_config_json():
42 """Generate Pyright config to be used in `pyrightconfig.json`."""
43 apis_active
= get_api_status()
48 name
.upper(): is_active
49 for name
, is_active
in apis_active
.items()
55 def generate_pyright_config_toml():
56 """Generate a Pyright config to be used in `pyproject.toml`."""
57 apis_active
= get_api_status()
59 return "[tool.pyright.defineConstant]\n" + "\n".join(
60 f
"{name.upper()} = {str(is_active).lower()}"
61 for name
, is_active
in apis_active
.items()
65 def print_mypy_args():
66 """Print the generated mypy args to stdout."""
67 print(generate_mypy_args())
70 def print_pyright_config_json():
71 """Print the generated Pyright JSON config to stdout."""
72 print(generate_pyright_config_json())
75 def print_pyright_config_toml():
76 """Print the generated Pyright TOML config to stdout."""
77 print(generate_pyright_config_toml())
80 def print_pyright_configs():
81 """Print the generated Pyright configs to stdout."""
82 print("pyrightconfig.json:")
83 print_pyright_config_json()
85 print("pyproject.toml:")
86 print_pyright_config_toml()
89 def generate_arg_parser():
90 """Generate the argument parser for the dev CLI for QtPy."""
91 parser
= argparse
.ArgumentParser(
92 description
="Features to support development with QtPy.",
94 parser
.set_defaults(func
=parser
.print_help
)
101 help="If passed, will print the version and exit",
104 cli_subparsers
= parser
.add_subparsers(
106 help="Subcommand to run",
107 metavar
="Subcommand",
110 # Parser for the MyPy args subcommand
111 mypy_args_parser
= cli_subparsers
.add_parser(
113 help="Generate command line arguments for using mypy with QtPy.",
114 formatter_class
=argparse
.RawTextHelpFormatter
,
115 description
=textwrap
.dedent(
117 Generate command line arguments for using mypy with QtPy.
119 This will generate strings similar to the following
120 which help guide mypy through which library QtPy would have used
121 so that mypy can get the proper underlying type hints.
123 --always-false=PYQT5 --always-false=PYQT6 --always-true=PYSIDE2 --always-false=PYSIDE6
125 It can be used as follows on Bash or a similar shell:
127 mypy --package mypackage $(qtpy mypy-args)
131 mypy_args_parser
.set_defaults(func
=print_mypy_args
)
133 # Parser for the Pyright config subcommand
134 pyright_config_parser
= cli_subparsers
.add_parser(
135 name
="pyright-config",
136 help="Generate Pyright config for using Pyright with QtPy.",
137 formatter_class
=argparse
.RawTextHelpFormatter
,
138 description
=textwrap
.dedent(
140 Generate Pyright config for using Pyright with QtPy.
142 This will generate config sections to be included in a Pyright
143 config file (either `pyrightconfig.json` or `pyproject.toml`)
144 which help guide Pyright through which library QtPy would have used
145 so that Pyright can get the proper underlying type hints.
150 pyright_config_parser
.set_defaults(func
=print_pyright_configs
)
156 """Run the development CLI for QtPy."""
157 parser
= generate_arg_parser()
158 parsed_args
= parser
.parse_args(args
=args
)
160 reserved_params
= {"func"}
163 for key
, value
in vars(parsed_args
).items()
164 if key
not in reserved_params
166 parsed_args
.func(**cleaned_args
)