Skip to content

Commit 6355433

Browse files
authored
Manage black version using requirement file (#1748)
* chore: Manage black version in dev.txt - config pre-commit - config development guide - config travis Refer to the commit below in aws-sam-cli aws/aws-sam-cli@d725db5fbfc698a9f0c7582 * Format using black 20.8b1 * Opt-out black fron dev.txt for Python 2
1 parent 18e7a7b commit 6355433

File tree

19 files changed

+131
-68
lines changed

19 files changed

+131
-68
lines changed

.pre-commit-config.yaml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
# https://pre-commit.com/#repository-local-hooks
12
repos:
2-
- repo: https://github.com/python/black
3-
rev: 19.3b0
4-
hooks:
5-
- id: black
6-
language_version: python3.7
7-
exclude_types: ['markdown', 'ini', 'toml', 'rst']
3+
- repo: local
4+
hooks:
5+
- id: black
6+
name: black
7+
entry: black
8+
language: system
9+
types: [python]

.travis.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ matrix:
2121

2222
install:
2323
# Install the code requirements
24-
- mkdir $HOME/bin-black
25-
- wget -O $HOME/bin-black/black https://github.com/python/black/releases/download/19.10b0/black
26-
- chmod +x $HOME/bin-black/black
27-
- export PATH=$PATH:$HOME/bin-black
28-
- black --version
29-
3024
- make init
3125

3226
# Install Docs requirements

DEVELOPMENT_GUIDE.rst

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,44 @@ Setup Python locally using `pyenv`_
2828

2929
2. Install Additional Tooling
3030
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31-
1. Black
32-
~~~~~~~~
31+
Black
32+
'''''
3333
We format our code using `Black`_ and verify the source code is black compliant
34-
in Appveyor during PRs. You can find installation instructions on `Black's docs`_.
34+
in Appveyor during PRs. Black will be installed automatically with ``make init``.
3535

36-
After installing, you can check your formatting through our Makefile by running `make black-check`. To automatically update your code to match our formatting, please run `make black`. You can also integrate Black directly in your favorite IDE (instructions
37-
can be found `here`_)
36+
After installing, you can run our formatting through our Makefile by
37+
``make black`` or integrating Black directly in your favorite IDE
38+
(instructions can be found `here <https://black.readthedocs.io/en/stable/editor_integration.html>`__)
39+
40+
(workaround) Integrating Black directly in your favorite IDE
41+
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
42+
43+
Since black is installed in virtualenv, when you follow `this
44+
instruction <https://black.readthedocs.io/en/stable/editor_integration.html>`__,
45+
``which black`` might give you this
46+
47+
::
48+
49+
(samtranslator37) $ where black
50+
/Users/<username>/.pyenv/shims/black
51+
52+
However, IDEs such as PyCharm (using FileWatcher) will have a hard time
53+
invoking ``/Users/<username>/.pyenv/shims/black`` and this will happen:
54+
55+
::
56+
57+
pyenv: black: command not found
58+
59+
The `black' command exists in these Python versions:
60+
3.7.2/envs/samtranslator37
61+
samtranslator37
62+
63+
A simple workaround is to use
64+
``/Users/<username>/.pyenv/versions/samtranslator37/bin/black`` instead of
65+
``/Users/<username>/.pyenv/shims/black``.
3866

3967
Pre-commit
40-
~~~~~~~~~~
68+
''''''''''
4169
If you don't wish to manually run black on each pr or install black manually, we have integrated black into git hooks through `pre-commit`_.
4270
After installing pre-commit, run `pre-commit install` in the root of the project. This will install black for you and run the black formatting on
4371
commit.

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ dev: test
2020
# Verifications to run before sending a pull request
2121
pr: black-check init dev
2222

23+
# Verifications to run before sending a pull request, skipping black check because black requires Python 3.6+
24+
pr2.7: init dev
25+
2326
define HELP_MESSAGE
2427

2528
Usage: $ make [TARGETS]

requirements/dev.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ requests>=2.20.0
1515

1616
# CLI requirements
1717
docopt>=0.6.2
18+
19+
# formatter
20+
black==20.8b1; python_version >= '3.6'

samtranslator/model/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,7 @@ def to_cloudformation(self, **kwargs):
355355

356356

357357
class SamResourceMacro(ResourceMacro):
358-
"""ResourceMacro that specifically refers to SAM (AWS::Serverless::*) resources.
359-
"""
358+
"""ResourceMacro that specifically refers to SAM (AWS::Serverless::*) resources."""
360359

361360
# SAM resources can provide a list of properties that they expose. These properties usually resolve to
362361
# CFN resources that this SAM resource generates. This is provided as a map with the following format:

samtranslator/model/api/http_api_generator.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ def _construct_api_domain(self, http_api):
216216
self.domain["EndpointConfiguration"] = "REGIONAL"
217217
elif endpoint not in ["REGIONAL"]:
218218
raise InvalidResourceException(
219-
self.logical_id, "EndpointConfiguration for Custom Domains must be one of {}.".format(["REGIONAL"]),
219+
self.logical_id,
220+
"EndpointConfiguration for Custom Domains must be one of {}.".format(["REGIONAL"]),
220221
)
221222
domain_config["EndpointType"] = endpoint
222223
domain_config["CertificateArn"] = self.domain.get("CertificateArn")
@@ -352,7 +353,8 @@ def _construct_alias_target(self, domain):
352353
alias_target["DNSName"] = fnGetAtt(self.domain.get("ApiDomainName"), "RegionalDomainName")
353354
else:
354355
raise InvalidResourceException(
355-
self.logical_id, "Only REGIONAL endpoint is supported on HTTP APIs.",
356+
self.logical_id,
357+
"Only REGIONAL endpoint is supported on HTTP APIs.",
356358
)
357359
return alias_target
358360

samtranslator/model/iam.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ def construct_assume_role_policy_for_service_principal(cls, service_principal):
2323
document = {
2424
"Version": "2012-10-17",
2525
"Statement": [
26-
{"Action": ["sts:AssumeRole"], "Effect": "Allow", "Principal": {"Service": [service_principal]},}
26+
{
27+
"Action": ["sts:AssumeRole"],
28+
"Effect": "Allow",
29+
"Principal": {"Service": [service_principal]},
30+
}
2731
],
2832
}
2933
return document
@@ -43,7 +47,11 @@ def stepfunctions_assume_role_policy(cls):
4347
document = {
4448
"Version": "2012-10-17",
4549
"Statement": [
46-
{"Action": ["sts:AssumeRole"], "Effect": "Allow", "Principal": {"Service": ["states.amazonaws.com"]},}
50+
{
51+
"Action": ["sts:AssumeRole"],
52+
"Effect": "Allow",
53+
"Principal": {"Service": ["states.amazonaws.com"]},
54+
}
4755
],
4856
}
4957
return document

samtranslator/model/lambda_.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ class LambdaEventInvokeConfig(Resource):
9999

100100

101101
class LambdaLayerVersion(Resource):
102-
""" Lambda layer version resource
103-
"""
102+
"""Lambda layer version resource"""
104103

105104
resource_type = "AWS::Lambda::LayerVersion"
106105
property_types = {

samtranslator/model/sam_resources.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@
4949

5050

5151
class SamFunction(SamResourceMacro):
52-
"""SAM function macro.
53-
"""
52+
"""SAM function macro."""
5453

5554
resource_type = "AWS::Serverless::Function"
5655
property_types = {
@@ -697,8 +696,7 @@ def _validate_deployment_preference_and_add_update_policy(
697696

698697

699698
class SamApi(SamResourceMacro):
700-
"""SAM rest API macro.
701-
"""
699+
"""SAM rest API macro."""
702700

703701
resource_type = "AWS::Serverless::Api"
704702
property_types = {
@@ -810,8 +808,7 @@ def to_cloudformation(self, **kwargs):
810808

811809

812810
class SamHttpApi(SamResourceMacro):
813-
"""SAM rest API macro.
814-
"""
811+
"""SAM rest API macro."""
815812

816813
resource_type = "AWS::Serverless::HttpApi"
817814
property_types = {
@@ -876,7 +873,13 @@ def to_cloudformation(self, **kwargs):
876873
disable_execute_api_endpoint=self.DisableExecuteApiEndpoint,
877874
)
878875

879-
(http_api, stage, domain, basepath_mapping, route53,) = api_generator.to_cloudformation()
876+
(
877+
http_api,
878+
stage,
879+
domain,
880+
basepath_mapping,
881+
route53,
882+
) = api_generator.to_cloudformation()
880883

881884
resources.append(http_api)
882885
if domain:
@@ -894,8 +897,7 @@ def to_cloudformation(self, **kwargs):
894897

895898

896899
class SamSimpleTable(SamResourceMacro):
897-
"""SAM simple table macro.
898-
"""
900+
"""SAM simple table macro."""
899901

900902
resource_type = "AWS::Serverless::SimpleTable"
901903
property_types = {
@@ -954,8 +956,7 @@ def _convert_attribute_type(self, attribute_type):
954956

955957

956958
class SamApplication(SamResourceMacro):
957-
"""SAM application macro.
958-
"""
959+
"""SAM application macro."""
959960

960961
APPLICATION_ID_KEY = "ApplicationId"
961962
SEMANTIC_VERSION_KEY = "SemanticVersion"
@@ -973,14 +974,12 @@ class SamApplication(SamResourceMacro):
973974
}
974975

975976
def to_cloudformation(self, **kwargs):
976-
"""Returns the stack with the proper parameters for this application
977-
"""
977+
"""Returns the stack with the proper parameters for this application"""
978978
nested_stack = self._construct_nested_stack()
979979
return [nested_stack]
980980

981981
def _construct_nested_stack(self):
982-
"""Constructs a AWS::CloudFormation::Stack resource
983-
"""
982+
"""Constructs a AWS::CloudFormation::Stack resource"""
984983
nested_stack = NestedStack(
985984
self.logical_id, depends_on=self.depends_on, attributes=self.get_passthrough_resource_attributes()
986985
)
@@ -994,8 +993,7 @@ def _construct_nested_stack(self):
994993
return nested_stack
995994

996995
def _get_application_tags(self):
997-
"""Adds tags to the stack if this resource is using the serverless app repo
998-
"""
996+
"""Adds tags to the stack if this resource is using the serverless app repo"""
999997
application_tags = {}
1000998
if isinstance(self.Location, dict):
1001999
if self.APPLICATION_ID_KEY in self.Location.keys() and self.Location[self.APPLICATION_ID_KEY] is not None:
@@ -1009,8 +1007,7 @@ def _get_application_tags(self):
10091007

10101008

10111009
class SamLayerVersion(SamResourceMacro):
1012-
""" SAM Layer macro
1013-
"""
1010+
"""SAM Layer macro"""
10141011

10151012
resource_type = "AWS::Serverless::LayerVersion"
10161013
property_types = {
@@ -1108,8 +1105,7 @@ def _get_retention_policy_value(self):
11081105

11091106

11101107
class SamStateMachine(SamResourceMacro):
1111-
"""SAM state machine macro.
1112-
"""
1108+
"""SAM state machine macro."""
11131109

11141110
resource_type = "AWS::Serverless::StateMachine"
11151111
property_types = {
@@ -1125,7 +1121,9 @@ class SamStateMachine(SamResourceMacro):
11251121
"Policies": PropertyType(False, one_of(is_str(), list_of(one_of(is_str(), is_type(dict), is_type(dict))))),
11261122
"Tracing": PropertyType(False, is_type(dict)),
11271123
}
1128-
event_resolver = ResourceTypeResolver(samtranslator.model.stepfunctions.events,)
1124+
event_resolver = ResourceTypeResolver(
1125+
samtranslator.model.stepfunctions.events,
1126+
)
11291127

11301128
def to_cloudformation(self, **kwargs):
11311129
managed_policy_map = kwargs.get("managed_policy_map", {})

0 commit comments

Comments
 (0)