-
-
Notifications
You must be signed in to change notification settings - Fork 636
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support mypy plugins and 3rdpary type definitions.
Add support to the Pants mypy contrib plugin for loading type definitions and mypy plugins from requirements in the transitive closure of targets being checked. Fixes #8263.
- Loading branch information
Showing
12 changed files
with
201 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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,42 @@ | ||
# Copyright 2019 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
python_requirement_library( | ||
name='django', | ||
requirements=[ | ||
python_requirement('Django==2.2.5'), | ||
] | ||
) | ||
|
||
python_requirement_library( | ||
name='django-stubs', | ||
requirements=[ | ||
python_requirement('django-stubs==1.1.0'), | ||
] | ||
) | ||
|
||
python_library( | ||
name='settings', | ||
source='settings.py', | ||
dependencies=[ | ||
':django-stubs', | ||
], | ||
) | ||
|
||
python_library( | ||
name='valid', | ||
source='valid.py', | ||
dependencies=[ | ||
':django', | ||
':settings', | ||
], | ||
) | ||
|
||
python_library( | ||
name='invalid', | ||
source='invalid.py', | ||
dependencies=[ | ||
':django', | ||
':settings', | ||
], | ||
) |
File renamed without changes.
This file contains 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,7 @@ | ||
# Copyright 2019 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from django.utils import text | ||
|
||
|
||
assert '42' == text.slugify(42) |
This file contains 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,6 @@ | ||
[mypy] | ||
plugins = | ||
mypy_django_plugin.main | ||
|
||
[mypy.plugins.django-stubs] | ||
django_settings_module = mypy_plugin.settings |
This file contains 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,11 @@ | ||
# Copyright 2019 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from django.urls import URLPattern | ||
|
||
|
||
DEBUG: bool = True | ||
DEFAULT_FROM_EMAIL: str = 'webmaster@example.com' | ||
SECRET_KEY: str = 'not so secret' | ||
|
||
MY_SETTING: URLPattern = URLPattern(pattern='foo', callback=lambda: None) |
This file contains 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,7 @@ | ||
# Copyright 2019 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from django.utils import text | ||
|
||
|
||
assert 'forty-two' == text.slugify('forty two') |
File renamed without changes.
Empty file.
File renamed without changes.
File renamed without changes.
This file contains 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
43 changes: 41 additions & 2 deletions
43
contrib/mypy/tests/python/pants_test/contrib/mypy/tasks/test_mypy_integration.py
This file contains 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 |
---|---|---|
@@ -1,17 +1,56 @@ | ||
# Copyright 2017 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from pathlib import Path | ||
|
||
from pants_test.pants_run_integration_test import PantsRunIntegrationTest | ||
|
||
|
||
class MypyIntegrationTest(PantsRunIntegrationTest): | ||
|
||
cmdline = ['--backend-packages=pants.contrib.mypy', 'lint'] | ||
|
||
def target(self, name): | ||
return f'contrib/mypy/examples/src/python/simple:{name}' | ||
|
||
def test_valid_type_hints(self): | ||
result = self.run_pants([*self.cmdline, 'contrib/mypy/examples/src/python:valid']) | ||
result = self.run_pants([*self.cmdline, self.target('valid')]) | ||
self.assert_success(result) | ||
|
||
def test_invalid_type_hints(self): | ||
result = self.run_pants([*self.cmdline, 'contrib/mypy/examples/src/python:invalid']) | ||
result = self.run_pants([*self.cmdline, self.target('invalid')]) | ||
self.assert_failure(result) | ||
|
||
|
||
class MypyPluginIntegrationTest(PantsRunIntegrationTest): | ||
|
||
example_dir = Path('contrib/mypy/examples/src/python/mypy_plugin') | ||
|
||
@classmethod | ||
def cmdline(cls, *, include_requirements): | ||
cmd = [ | ||
'--backend-packages=pants.contrib.mypy', | ||
'lint.mypy', | ||
f'--config-file={cls.example_dir / "mypy.ini"}' | ||
] | ||
if include_requirements: | ||
cmd.append('--include-requirements') | ||
return cmd | ||
|
||
@classmethod | ||
def target(cls, name): | ||
return f'{cls.example_dir}:{name}' | ||
|
||
def test_valid_library_use_include_requirements(self): | ||
result = self.run_pants([*self.cmdline(include_requirements=True), self.target('valid')]) | ||
self.assert_success(result) | ||
|
||
def test_invalid_library_use_include_requirements(self): | ||
result = self.run_pants([*self.cmdline(include_requirements=True), self.target('invalid')]) | ||
self.assert_failure(result) | ||
|
||
def test_valid_library_use_exclude_requirements(self): | ||
# The target is valid, but we fail to include the mypy plugin and type information needed via | ||
# requirements and so the check fails. | ||
result = self.run_pants([*self.cmdline(include_requirements=False), self.target('valid')]) | ||
self.assert_failure(result) |