-
-
Notifications
You must be signed in to change notification settings - Fork 163
PR: Add command line support for Mypy #337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1d4131f
Preliminary cli for mypy options
altendky 0139266
Fix, clean up and improve new CLI tests
CAM-Gerlach f341da2
Simpifly and improve CLI for Mypy
CAM-Gerlach 200f5f2
Update and improve Readme and help text for mypy-args CLI command
CAM-Gerlach 83f3cb6
Add version option to QtPy CLI
CAM-Gerlach c18fcf3
Further improve Mypy section in Readme & add license header/docstring
CAM-Gerlach cec7c82
Further refine Mypy CLI in readme from reviewer feedback
CAM-Gerlach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,3 +5,4 @@ include README* | |
| include SECURITY* | ||
| include pytest.ini | ||
| recursive-include qtpy/tests *.py *.ui | ||
| include qtpy/py.typed | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # ----------------------------------------------------------------------------- | ||
| # Copyright © 2009- The QtPy Contributors | ||
| # | ||
| # Released under the terms of the MIT License | ||
| # (see LICENSE.txt for details) | ||
| # ----------------------------------------------------------------------------- | ||
|
|
||
| """Dev CLI entry point for QtPy, a compat layer for the Python Qt bindings.""" | ||
|
|
||
| import qtpy.cli | ||
dalthviz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def main(): | ||
| return qtpy.cli.main() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| # ----------------------------------------------------------------------------- | ||
| # Copyright © 2009- The QtPy Contributors | ||
| # | ||
| # Released under the terms of the MIT License | ||
| # (see LICENSE.txt for details) | ||
| # ----------------------------------------------------------------------------- | ||
|
|
||
| """Provide a CLI to allow configuring developer settings, including mypy.""" | ||
|
|
||
| # Standard library imports | ||
| import argparse | ||
| import textwrap | ||
|
|
||
|
|
||
| def print_version(): | ||
| """Print the current version of the package.""" | ||
| import qtpy | ||
| print('QtPy version', qtpy.__version__) | ||
|
|
||
|
|
||
| def generate_mypy_args(): | ||
| """Generate a string with always-true/false args to pass to mypy.""" | ||
| options = {False: '--always-false', True: '--always-true'} | ||
|
|
||
| import qtpy | ||
|
|
||
| apis_active = {name: qtpy.API == name for name in qtpy.API_NAMES} | ||
| mypy_args = ' '.join( | ||
| f'{options[is_active]}={name.upper()}' | ||
| for name, is_active in apis_active.items() | ||
| ) | ||
| return mypy_args | ||
|
|
||
|
|
||
| def print_mypy_args(): | ||
| """Print the generated mypy args to stdout.""" | ||
| print(generate_mypy_args()) | ||
|
|
||
|
|
||
| def generate_arg_parser(): | ||
| """Generate the argument parser for the dev CLI for QtPy.""" | ||
| parser = argparse.ArgumentParser( | ||
| description='Features to support development with QtPy.', | ||
| ) | ||
| parser.set_defaults(func=parser.print_help) | ||
|
|
||
| parser.add_argument( | ||
| '--version', action='store_const', dest='func', const=print_version, | ||
| help='If passed, will print the version and exit') | ||
|
|
||
| cli_subparsers = parser.add_subparsers( | ||
| title='Subcommands', help='Subcommand to run', metavar='Subcommand') | ||
|
|
||
| # Parser for the MyPy args subcommand | ||
| mypy_args_parser = cli_subparsers.add_parser( | ||
| name='mypy-args', | ||
| help='Generate command line arguments for using mypy with QtPy.', | ||
| formatter_class=argparse.RawTextHelpFormatter, | ||
| description=textwrap.dedent( | ||
| """ | ||
| Generate command line arguments for using mypy with QtPy. | ||
|
|
||
| This will generate strings similar to the following | ||
| which help guide mypy through which library QtPy would have used | ||
| so that mypy can get the proper underlying type hints. | ||
|
|
||
| --always-false=PYQT5 --always-false=PYQT6 --always-true=PYSIDE2 --always-false=PYSIDE6 | ||
|
|
||
| It can be used as follows on Bash or a similar shell: | ||
|
|
||
| mypy --package mypackage $(qtpy mypy-args) | ||
| """ | ||
| ), | ||
| ) | ||
| mypy_args_parser.set_defaults(func=print_mypy_args) | ||
|
|
||
| return parser | ||
|
|
||
|
|
||
| def main(args=None): | ||
| """Run the development CLI for QtPy.""" | ||
| parser = generate_arg_parser() | ||
| parsed_args = parser.parse_args(args=args) | ||
|
|
||
| reserved_params = {'func'} | ||
| cleaned_args = {key: value for key, value in vars(parsed_args).items() | ||
| if key not in reserved_params} | ||
| parsed_args.func(**cleaned_args) |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| """Test the QtPy CLI.""" | ||
|
|
||
| import subprocess | ||
| import sys | ||
|
|
||
| import pytest | ||
|
|
||
| import qtpy | ||
|
|
||
|
|
||
| SUBCOMMANDS = [ | ||
| [], | ||
| ['mypy-args'], | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| argnames=['subcommand'], | ||
| argvalues=[[subcommand] for subcommand in SUBCOMMANDS], | ||
| ids=[' '.join(subcommand) for subcommand in SUBCOMMANDS], | ||
| ) | ||
| def test_cli_help_does_not_fail(subcommand): | ||
| subprocess.run( | ||
| [sys.executable, '-m', 'qtpy', *subcommand, '--help'], check=True, | ||
| ) | ||
|
|
||
|
|
||
| def test_cli_version(): | ||
| output = subprocess.run( | ||
| [sys.executable, '-m', 'qtpy', '--version'], | ||
| capture_output=True, | ||
| check=True, | ||
| encoding='utf-8', | ||
| ) | ||
| assert output.stdout.strip().split()[-1] == qtpy.__version__ | ||
|
|
||
|
|
||
| def test_cli_mypy_args(): | ||
| output = subprocess.run( | ||
| [sys.executable, '-m', 'qtpy', 'mypy-args'], | ||
| capture_output=True, | ||
| check=True, | ||
| encoding='utf-8', | ||
| ) | ||
|
|
||
| if qtpy.PYQT5: | ||
| expected = ' '.join([ | ||
| '--always-true=PYQT5', | ||
| '--always-false=PYQT6', | ||
| '--always-false=PYSIDE2', | ||
| '--always-false=PYSIDE6', | ||
| ]) | ||
| elif qtpy.PYQT6: | ||
| expected = ' '.join([ | ||
| '--always-false=PYQT5', | ||
| '--always-true=PYQT6', | ||
| '--always-false=PYSIDE2', | ||
| '--always-false=PYSIDE6', | ||
| ]) | ||
| elif qtpy.PYSIDE2: | ||
| expected = ' '.join([ | ||
| '--always-false=PYQT5', | ||
| '--always-false=PYQT6', | ||
| '--always-true=PYSIDE2', | ||
| '--always-false=PYSIDE6', | ||
| ]) | ||
| elif qtpy.PYSIDE6: | ||
| expected = ' '.join([ | ||
| '--always-false=PYQT5', | ||
| '--always-false=PYQT6', | ||
| '--always-false=PYSIDE2', | ||
| '--always-true=PYSIDE6', | ||
| ]) | ||
| else: | ||
| assert False, 'No valid API to test' | ||
|
|
||
| assert output.stdout.strip() == expected.strip() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.