Skip to content

Commit c98937b

Browse files
authored
feat: add pre-commit hooks (#64)
* chore: pre-commit dep Signed-off-by: heitorlessa <[email protected]> * feat: add initial pre-commit config Signed-off-by: heitorlessa <[email protected]> * feat: additional type annotation, toml, merge, etc Signed-off-by: heitorlessa <[email protected]> * improv: speed up formatting Signed-off-by: heitorlessa <[email protected]> * fix: python version range Signed-off-by: heitorlessa <[email protected]> * chore: pre-commit checks * chore: update dev deps * feat: install pre-commit hooks during dev Signed-off-by: heitorlessa <[email protected]>
1 parent 993e8e5 commit c98937b

File tree

26 files changed

+180
-144
lines changed

26 files changed

+180
-144
lines changed

Diff for: .github/ISSUE_TEMPLATE/bug_report.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ assignees: ''
3636

3737
* **Powertools version used**:
3838
* **Packaging format (Layers, PyPi)**:
39-
* **AWS Lambda function runtime:**
39+
* **AWS Lambda function runtime:**
4040
* **Debugging logs**
4141

4242
> [How to enable debug mode](https://awslabs.github.io/aws-lambda-powertools-python/#debug-mode)**

Diff for: .github/ISSUE_TEMPLATE/rfc.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ assignees: ''
1010
## Key information
1111

1212
* RFC PR: (leave this empty)
13-
* Related issue(s), if known:
13+
* Related issue(s), if known:
1414
* Area: (i.e. Tracer, Metrics, Logger, etc.)
1515
* Meet [tenets](https://awslabs.github.io/aws-lambda-powertools-python/#tenets): (Yes/no)
1616

Diff for: .pre-commit-config.yaml

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# We use poetry to run formatting and linting before commit/push
2+
# Longers checks such as tests, security and complexity baseline
3+
# are run as part of CI to prevent slower feedback loop
4+
# All checks can be run locally via `make pr`
5+
6+
repos:
7+
- repo: https://github.com/pre-commit/pre-commit-hooks
8+
rev: v2.4.0
9+
hooks:
10+
- id: check-merge-conflict
11+
- id: trailing-whitespace
12+
- id: end-of-file-fixer
13+
- id: check-toml
14+
- repo: https://github.com/pre-commit/pygrep-hooks
15+
rev: v1.5.1
16+
hooks:
17+
- id: python-use-type-annotations
18+
- repo: local
19+
hooks:
20+
- id: black
21+
name: formatting::black
22+
entry: poetry run black
23+
language: system
24+
types: [python]
25+
- id: isort
26+
name: formatting::isort
27+
entry: poetry run isort -rc
28+
language: system
29+
types: [python]
30+
- repo: local
31+
hooks:
32+
- id: flake8
33+
name: linting::flake8
34+
entry: poetry run flake8
35+
language: system
36+
types: [python]
37+
exclude: example
38+
fail_fast: true

Diff for: CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Changelog
1+
# Changelog
22
All notable changes to this project will be documented in this file.
33

44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

Diff for: LICENSE

-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,3 @@ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
1212
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
1313
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1414
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15-

Diff for: Makefile

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ target:
33
@$(MAKE) pr
44

55
dev:
6-
pip install --upgrade pip poetry
6+
pip install --upgrade pip poetry pre-commit
77
poetry install
8+
pre-commit install
89

910
dev-docs:
1011
cd docs && npm install
1112

1213
format:
13-
poetry run isort -rc .
14-
poetry run black aws_lambda_powertools
15-
poetry run black tests
14+
poetry run isort -rc aws_lambda_powertools tests example
15+
poetry run black aws_lambda_powertools tests
1616

1717
lint: format
1818
poetry run flake8

Diff for: aws_lambda_powertools/metrics/schema.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,4 @@
111111
}
112112
}
113113
}
114-
}
114+
}

Diff for: aws_lambda_powertools/middleware_factory/factory.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def lambda_handler_decorator(decorator: Callable = None, trace_execution=False):
2727
Environment variables
2828
---------------------
2929
POWERTOOLS_TRACE_MIDDLEWARES : str
30-
uses `aws_lambda_powertools.tracing.Tracer`
30+
uses `aws_lambda_powertools.tracing.Tracer`
3131
to create sub-segments per middleware (e.g. `"true", "True", "TRUE"`)
3232
3333
Parameters

Diff for: bandit.baseline

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,4 @@
223223
"test_name": "blacklist"
224224
}
225225
]
226-
}
226+
}

Diff for: docs/content/core/logger.mdx

+9-9
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ You can also explicitly set a service name via `service` param or via `POWERTOOL
3939
```python:title=app.py
4040
from aws_lambda_powertools.logging import Logger
4141
# POWERTOOLS_SERVICE_NAME defined
42-
logger = Logger() # highlight-line
42+
logger = Logger() # highlight-line
4343

4444
# Explicit definition
4545
Logger(service="payment", level="INFO")
@@ -58,7 +58,7 @@ Key | Type | Example | Description
5858
**sampling_rate** | int | 0.1 | Debug logging sampling rate in percentage e.g. 1% in this case
5959
**message** | any | "Collecting payment" | Log statement value. Unserializable JSON values will be casted to string
6060

61-
## Capturing context Lambda info
61+
## Capturing context Lambda info
6262

6363
You can enrich your structured logs with key Lambda context information via `inject_lambda_context`.
6464

@@ -109,7 +109,7 @@ Key | Type | Example
109109
<summary><strong>Exerpt output in CloudWatch Logs</strong></summary>
110110

111111
```json:title=cloudwatch_logs.json
112-
{
112+
{
113113
"timestamp":"2020-05-24 18:17:33,774",
114114
"level":"INFO",
115115
"location":"collect.handler:1",
@@ -125,7 +125,7 @@ Key | Type | Example
125125
"message": "Collecting payment"
126126
}
127127

128-
{
128+
{
129129
"timestamp":"2020-05-24 18:17:33,774",
130130
"level":"INFO",
131131
"location":"collect.handler:15",
@@ -137,7 +137,7 @@ Key | Type | Example
137137
"cold_start": true,
138138
"sampling_rate": 0.0,
139139
// highlight-start
140-
"message":{
140+
"message":{
141141
"operation":"collect_payment",
142142
"charge_id": "ch_AZFlk2345C0"
143143
}
@@ -148,7 +148,7 @@ Key | Type | Example
148148

149149
## Appending additional keys
150150

151-
You can append your own keys to your existing Logger via `structure_logs` with **append** param.
151+
You can append your own keys to your existing Logger via `structure_logs` with **append** param.
152152

153153
```python:title=collect.py
154154
from aws_lambda_powertools.logging import Logger
@@ -166,7 +166,7 @@ def handler(event, context)
166166
<summary><strong>Exerpt output in CloudWatch Logs</strong></summary>
167167

168168
```json:title=cloudwatch_logs.jsonn
169-
{
169+
{
170170
"timestamp": "2020-05-24 18:17:33,774",
171171
"level": "INFO",
172172
"location": "collect.handler:1",
@@ -209,7 +209,7 @@ def handler(event, context)
209209
<summary><strong>Exerpt output in CloudWatch Logs</strong></summary>
210210

211211
```json:title=cloudwatch_logs.json
212-
{
212+
{
213213
"timestamp": "2020-05-24 18:17:33,774",
214214
"level": "INFO",
215215
"location": "collect.handler:1",
@@ -223,4 +223,4 @@ def handler(event, context)
223223
"message": "Collecting payment"
224224
}
225225
```
226-
</details>
226+
</details>

Diff for: docs/content/core/metrics.mdx

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ You can also pass a service name via `service` param or `POWERTOOLS_SERVICE_NAME
3939
from aws_lambda_powertools.metrics import Metrics, MetricUnit
4040

4141
# POWERTOOLS_METRICS_NAMESPACE and POWERTOOLS_SERVICE_NAME defined
42-
metrics = Metrics() # highlight-line
42+
metrics = Metrics() # highlight-line
4343

4444
# Explicit definition
4545
Metrics(namespace="ServerlessAirline", service="orders") # creates a default dimension {"service": "orders"} under the namespace "ServerlessAirline"
@@ -69,7 +69,7 @@ CloudWatch EMF supports a max of 100 metrics. Metrics will automatically flush a
6969

7070
## Creating a metric with a different dimension
7171

72-
CloudWatch EMF uses the same dimensions across all your metrics. Use `single_metric` if you have a metric that should have different dimensions.
72+
CloudWatch EMF uses the same dimensions across all your metrics. Use `single_metric` if you have a metric that should have different dimensions.
7373

7474
<Note type="info">
7575
Generally, this would be an edge case since you <a href="https://aws.amazon.com/cloudwatch/pricing/">pay for unique metric</a>. Keep the following formula in mind:
@@ -105,7 +105,7 @@ def lambda_handler(evt, ctx):
105105

106106
`log_metrics` decorator **validates**, **serializes**, and **flushes** all your metrics. During metrics validation, if any of the following criteria is met, `SchemaValidationError` exception will be raised:
107107

108-
* At least of one Metric and Dimension
108+
* At least of one Metric and Dimension
109109
* Maximum of 9 dimensions
110110
* Namespace is set, and no more than one
111111
* Metric units must be [supported by CloudWatch](https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_MetricDatum.html)
@@ -141,7 +141,7 @@ metrics = Metrics(namespace="ExampleApplication", service="booking")
141141
metrics.add_metric(name="ColdStart", unit="Count", value=1)
142142

143143
# highlight-start
144-
your_metrics_object = metrics.serialize_metric_set()
144+
your_metrics_object = metrics.serialize_metric_set()
145145
metrics.clear_metrics()
146146
print(json.dumps(your_metrics_object))
147147
# highlight-end

Diff for: docs/content/core/tracer.mdx

+8-8
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ You can either explicitly pass using `service` param or via `POWERTOOLS_SERVICE_
3939
```python:title=app.py
4040
from aws_lambda_powertools.tracing import Tracer
4141
# POWERTOOLS_SERVICE_NAME defined
42-
tracer = Tracer() # highlight-line
42+
tracer = Tracer() # highlight-line
4343

4444
# Explicit definition
4545
tracer = Tracer(service="booking")
4646
```
4747

4848
## Lambda handler
4949

50-
You can trace your Lambda function handler via `capture_lambda_handler`.
50+
You can trace your Lambda function handler via `capture_lambda_handler`.
5151

5252
When using this decorator, Tracer performs these additional tasks to ease operations:
5353

@@ -56,7 +56,7 @@ When using this decorator, Tracer performs these additional tasks to ease operat
5656

5757
```python:title=lambda_handler.py
5858
from aws_lambda_powertools.tracing import Tracer
59-
tracer = Tracer()
59+
tracer = Tracer()
6060

6161
@tracer.capture_lambda_handler # highlight-line
6262
def handler(event, context)
@@ -73,7 +73,7 @@ You can add annotations using `put_annotation` method from Tracer.
7373

7474
```python:title=app.py
7575
from aws_lambda_powertools.tracing import Tracer
76-
tracer = Tracer()
76+
tracer = Tracer()
7777

7878
@tracer.capture_lambda_handler
7979
def handler(event, context):
@@ -89,7 +89,7 @@ You can add metadata using `put_metadata` method from Tracer.
8989

9090
```python:title=app.py
9191
from aws_lambda_powertools.tracing import Tracer
92-
tracer = Tracer()
92+
tracer = Tracer()
9393

9494
@tracer.capture_lambda_handler
9595
def handler(event, context):
@@ -125,7 +125,7 @@ from aws_lambda_powertools.tracing import Tracer
125125
tracer = Tracer()
126126

127127
# highlight-start
128-
@tracer.capture_method
128+
@tracer.capture_method
129129
async def collect_payment():
130130
...
131131
# highlight-end
@@ -194,7 +194,7 @@ async def collect_payment(charge_id):
194194
...
195195
```
196196

197-
## Reusing Tracer across your code
197+
## Reusing Tracer across your code
198198

199199
Tracer keeps a copy of its configuration after the first initialization. This is useful for scenarios where you want to use Tracer in more than one location across your code base.
200200

@@ -214,7 +214,7 @@ def handler(event, context)
214214
from aws_lambda_powertools.tracing import Tracer
215215
# highlight-start
216216
# new instance using existing configuration with auto patching overriden
217-
tracer = Tracer(auto_patch=False)
217+
tracer = Tracer(auto_patch=False)
218218
# highlight-end
219219
```
220220

Diff for: docs/content/dummy.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ description: Dummy description
55

66
## Test
77

8-
dummy content
8+
dummy content

Diff for: docs/content/index.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Powertools is available in PyPi. You can use your favourite dependency managemen
2323

2424
* **AWS Lambda only** – We optimise for AWS Lambda function environments and supported runtimes only. Utilities might work with web frameworks and non-Lambda environments, though they are not officially supported.
2525
* **Eases the adoption of best practices** – The main priority of the utilities is to facilitate best practices adoption, as defined in the AWS Well-Architected Serverless Lens; all other functionality is optional.
26-
* **Keep it lean** – Additional dependencies are carefully considered for security and ease of maintenance, and prevent negatively impacting startup time.
26+
* **Keep it lean** – Additional dependencies are carefully considered for security and ease of maintenance, and prevent negatively impacting startup time.
2727
* **We strive for backwards compatibility** – New features and changes should keep backwards compatibility. If a breaking change cannot be avoided, the deprecation and migration process should be clearly defined.
2828
* **We work backwards from the community** – We aim to strike a balance of what would work best for 80% of customers. Emerging practices are considered and discussed via Requests for Comment (RFCs)
2929
* **Idiomatic** – Utilities follow programming language idioms and language-specific best practices.

Diff for: docs/content/utilities/middleware_factory.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
2525

2626
# highlight-start
2727
@lambda_handler_decorator
28-
def middleware_before_after(handler, event, context):
28+
def middleware_before_after(handler, event, context):
2929
# highlight-end
3030
# logic_before_handler_execution()
3131
response = handler(event, context)

Diff for: docs/gatsby-config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ module.exports = {
6262
'AWS SAM Docs': {
6363
href: 'https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html',
6464
}
65-
}
65+
}
6666
}
6767
},
6868
{

Diff for: docs/src/assets/aws-logo.svg

+1-1
Loading

Diff for: docs/src/components/Note/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ const Note = ({ type = 'info', title = '', children }) => {
2020
)
2121
}
2222

23-
export default Note
23+
export default Note
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// Shadow Header built-in component and remove `Launch Graph Manager`
2-
export default () => null;
2+
export default () => null;

Diff for: example/.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -241,4 +241,4 @@ $RECYCLE.BIN/
241241

242242
*/build/*
243243

244-
# End of https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode
244+
# End of https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode

Diff for: example/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ This project contains source code and supporting files for a serverless applicat
1919

2020
- hello_world - Code for the application's Lambda function.
2121
- events - Invocation events that you can use to invoke the function.
22-
- tests - Unit tests for the application code.
22+
- tests - Unit tests for the application code.
2323
- template.yaml - A template that defines the application's AWS resources.
2424
- requirements-dev.txt - Dev dependencies to run unit tests successfully
2525

2626
The application uses several AWS resources, including Lambda functions and an API Gateway API. These resources are defined in the `template.yaml` file in this project. You can update the template to add AWS resources through the same deployment process that updates your application code.
2727

28-
If you prefer to use an integrated development environment (IDE) to build and test your application, you can use the AWS Toolkit.
28+
If you prefer to use an integrated development environment (IDE) to build and test your application, you can use the AWS Toolkit.
2929
The AWS Toolkit is an open source plug-in for popular IDEs that uses the SAM CLI to build and deploy serverless applications on AWS. The AWS Toolkit also adds a simplified step-through debugging experience for Lambda function code. See the following links to get started.
3030

3131
* [PyCharm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html)

Diff for: example/hello_world/app.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
_cold_start = True
2222

23-
metrics.add_dimension(name="operation", value="example") # added at cold start only
23+
metrics.add_dimension(name="operation", value="example") # added at cold start only
2424

2525

2626
async def aioboto_task():

Diff for: example/pytest.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[pytest]
22
addopts = -ra -v
3-
testpaths = ./tests
3+
testpaths = ./tests

0 commit comments

Comments
 (0)