Skip to content
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

Prep to Open Source #3

Merged
merged 29 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
38aa0f6
Small fixes for CI and a license
crimsonknave Sep 6, 2024
7f6d8ae
Split coverage action into two
crimsonknave Sep 23, 2024
d389102
Mega commit with ci fixes, and converting to use logging dict
crimsonknave Sep 26, 2024
0dd1713
Apparently, it was really mad I had a typo in the readme name
crimsonknave Sep 27, 2024
a3fcf2b
Not sure how that got uncapitalized
crimsonknave Oct 7, 2024
74814bc
Working on smushing mutations
crimsonknave Oct 7, 2024
09235ae
Cleaning up dependencies and fixing a mutation
crimsonknave Oct 7, 2024
9f6db68
Fixing up CI and mutations
crimsonknave Oct 7, 2024
29fedeb
Adding codeql
crimsonknave Oct 7, 2024
3cbe249
yaml >> yml
crimsonknave Oct 7, 2024
b9d11bd
Killing last mutation
crimsonknave Oct 7, 2024
0f95223
Adding/updating OSS documentation
crimsonknave Oct 7, 2024
56adb9b
Removing custom codeql scanning
crimsonknave Oct 9, 2024
f4f5cf2
Too large commit: Add actions plugin and lots of rework to solve some…
crimsonknave Oct 10, 2024
5bdda75
Adding note about merging logging configs
crimsonknave Oct 10, 2024
47ed59c
Removing mutmut from CI as it is not longer working as expected.
crimsonknave Nov 26, 2024
b847919
Minor cleanup
crimsonknave Nov 26, 2024
24a8ae2
Pin coverage action to a sha
crimsonknave Nov 26, 2024
200e0b9
Cleaning up some code scanning issues
crimsonknave Nov 26, 2024
23c94f6
Fixing up workflows
crimsonknave Nov 27, 2024
84a2086
Readme and coverage ci updates
crimsonknave Nov 27, 2024
e251cdb
Getting the coverage comment to work again
crimsonknave Nov 27, 2024
ba69eff
Fixing action yaml
crimsonknave Nov 27, 2024
28ccdc2
More fixing of CI
crimsonknave Nov 27, 2024
8de95a8
Maybe actually fix the coverage ci
crimsonknave Nov 27, 2024
a2f4d6a
Convert runtime annotations to plugin and additional testing for acti…
crimsonknave Dec 3, 2024
d183d62
Cleanup and removing some support for older configurations
crimsonknave Dec 4, 2024
10ea0dd
Fully test actions log message
crimsonknave Dec 4, 2024
6209b3a
More cleanup
crimsonknave Dec 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/workflows/coverage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

name: Post Coverage Commit

on:
workflow_run:
workflows: ["Pytest"]
types:
- completed

jobs:
coverage:
runs-on: ubuntu-latest
if: github.event.workflow_run.event == "pull_request" && github.event.workflow_run.conclusion == "success"
permissions:
pull-requests: write
contents: write
actions: read
steps:
- uses: actions/checkout@v4

- uses: actions/download-artifact@v4
id: download
with:
pattern: coverage-*
merge-multiple: true
- name: Re-add relative so the action is happy
run: |
echo "[run]" >> .coveragerc
echo "relative_files = true" >> .coveragerc
- name: Python Coverage Comment
uses: py-cov-action/[email protected]
with:
GITHUB_TOKEN: ${{ github.token }}
MERGE_COVERAGE_FILES: true
GITHUB_PR_RUN_ID: ${{ github.event.workflow_run.id }}
14 changes: 14 additions & 0 deletions .github/workflows/matchers/ruff.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"problemMatcher": [
{
"owner": "ruff",
"pattern": [
{
"regexp": "^(Would reformat): (.+)$",
"message": 1,
"file": 2
}
]
}
]
}
35 changes: 7 additions & 28 deletions .github/workflows/pytest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jobs:
pytest:

runs-on: ${{ matrix.os }}
permissions:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
Expand Down Expand Up @@ -40,32 +41,10 @@ jobs:
hatch run dev:pytest
env:
COVERAGE_FILE: ".coverage.${{ matrix.os }}.${{ matrix.python-version }}"
- name: Store coverage file
uses: actions/upload-artifact@v4
with:
name: coverage-${{ matrix.os }}-${{ matrix.python-version }}
path: .coverage.${{ matrix.os }}.${{ matrix.python-version }}
- name: Store Pull Request comment to be posted
uses: actions/upload-artifact@v4
if: steps.coverage_comment.outputs.COMMENT_FILE_WRITTEN == 'true'
with:
name: python-coverage-comment-action
path: python-coverage-comment-action.txt

coverage:
runs-on: ubuntu-latest
needs: pytest
permissions:
pull-requests: write
contents: write
steps:
- uses: actions/checkout@v4

- uses: actions/download-artifact@v4
id: download
with:
pattern: coverage-*
merge-multiple: true
- name: Re-add relative so the action is happy
run: |
echo "[run]" >> .coveragerc
echo "relative_files = true" >> .coveragerc
- name: Python Coverage Comment
uses: py-cov-action/[email protected]
with:
GITHUB_TOKEN: ${{ github.token }}
MERGE_COVERAGE_FILES: true
9 changes: 7 additions & 2 deletions .github/workflows/ruff.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ jobs:
run: |
python -m pip install hatch
hatch env create dev
- name: Lint with Ruff
- name: Lint with Ruff (check)
run: |
hatch run dev:ruff --output-format=github .
hatch run dev:ruff check --output-format=github .
- name: Register problem matcher for ruff format
run: echo "::add-matcher::.github/workflows/matchers/ruff.json"
- name: Lint with Ruff (format)
run: |
hatch run dev:ruff format --check .
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright GitHub, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 3 additions & 1 deletion annotated_logger/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ def __init__( # noqa: PLR0913
self.formatter = formatter or JsonFormatter(
"%(created)s %(levelname)s %(name)s %(message)s" # pragma: no mutate
)
self.handlers = [handler]
handler.setFormatter(self.formatter)
self.max_length = max_length

Expand All @@ -356,7 +357,8 @@ def _generate_logger(
f"{self.logger_root_name}.{uuid.uuid4()}" # pragma: no mutate
)

logger.addHandler(handler)
for handler_item in self.handlers:
logger.addHandler(handler_item)

annotated_filter = self.generate_filter(function=function, cls=cls)

Expand Down
3 changes: 2 additions & 1 deletion annotated_logger/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,9 @@


@pytest.fixture()
def annotated_logger_mock(mocker: MagicMock) -> AnnotatedLogMock:
def annotated_logger_mock(mocker: MagicMock, annotated_logger_object: AnnotatedLogger) -> AnnotatedLogMock:

Check failure on line 247 in annotated_logger/mocks.py

View workflow job for this annotation

GitHub Actions / pyright

"AnnotatedLogger" is not defined (reportUndefinedVariable)

Check failure on line 247 in annotated_logger/mocks.py

View workflow job for this annotation

GitHub Actions / ruff

annotated_logger/mocks.py:247:89: E501 Line too long (107 > 88)

Check failure on line 247 in annotated_logger/mocks.py

View workflow job for this annotation

GitHub Actions / ruff

annotated_logger/mocks.py:247:71: F821 Undefined name `AnnotatedLogger`

Check failure on line 247 in annotated_logger/mocks.py

View workflow job for this annotation

GitHub Actions / ruff

annotated_logger/mocks.py:247:46: ARG001 Unused function argument: `annotated_logger_object`
"""Fixture for a mock of the annotated logger."""
import pdb;pdb.set_trace()

Check failure on line 249 in annotated_logger/mocks.py

View workflow job for this annotation

GitHub Actions / ruff

annotated_logger/mocks.py:249:16: T100 Trace found: `pdb.set_trace` used

Check failure on line 249 in annotated_logger/mocks.py

View workflow job for this annotation

GitHub Actions / ruff

annotated_logger/mocks.py:249:15: E702 Multiple statements on one line (semicolon)

Check failure on line 249 in annotated_logger/mocks.py

View workflow job for this annotation

GitHub Actions / ruff

annotated_logger/mocks.py:249:5: T100 Import for `pdb` found

Check failure on line 249 in annotated_logger/mocks.py

View workflow job for this annotation

GitHub Actions / ruff

annotated_logger/mocks.py:249:5: I001 Import block is un-sorted or un-formatted
return mocker.patch(
"annotated_logger.handler",
new_callable=AnnotatedLogMock,
Expand Down
9 changes: 5 additions & 4 deletions example/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
return "this function is called every time"


annotated_logger = AnnotatedLogger(
ann_logger = AnnotatedLogger(
annotations={
"extra": "new data",
"nested_extra": {"nested_key": {"double_nested_key": "value"}},
Expand All @@ -32,10 +32,10 @@
RemoverPlugin("taskName"),
NestedRemoverPlugin(["double_nested_key"]),
],
name="annotated_logger.example",
name="annotated_logger.calculator",
)

annotate_logs = annotated_logger.annotate_logs
annotate_logs = ann_logger.annotate_logs

Number = int | float

Expand Down Expand Up @@ -90,7 +90,7 @@
"Prediction result", extra={"result": success != prediction}
)

@annotated_logger.annotate_logs(
@ann_logger.annotate_logs(
success_info=False,
pre_call=check_zero_division,
_typing_requested=True,
Expand All @@ -102,6 +102,7 @@
"If you divide by zero you'll create a singularity in the fabric of space-time!", # noqa: E501
extra={"joke": True},
)
import pdb;pdb.set_trace()

Check failure on line 105 in example/calculator.py

View workflow job for this annotation

GitHub Actions / ruff

example/calculator.py:105:19: E702 Multiple statements on one line (semicolon)

Check failure on line 105 in example/calculator.py

View workflow job for this annotation

GitHub Actions / ruff

example/calculator.py:105:9: I001 Import block is un-sorted or un-formatted

Check failure on line 105 in example/calculator.py

View workflow job for this annotation

GitHub Actions / ruff

example/calculator.py:105:9: T100 Import for `pdb` found
try:
return self.first / self.second
except ZeroDivisionError:
Expand Down
10 changes: 10 additions & 0 deletions notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Should support the ability to configure via a logging config.

The AnnotatedLogger class would take params that let it build/override parts of the config.

Need to sort out how we do the mock on the handler then. Have the mock fixture pull in a fixture for what handler to do and then if you don't do that you can override that fixture? How does it work with multiple handlers?


The overall goal is to be able to support the case where you want to configure multiple output streams (handlers) with different filters. AKA, output with a different log level that filters/formats for actions notifications at the same time as a normal logger

Also, move base_attribute support into the plugin base and also store the values we add in the annotated (annotated, action and so on). Then make a function that lets a plugin ask for the bits of a record that were added by the caller or the caller and the annotated_logger.
10 changes: 5 additions & 5 deletions test/test_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import test.demo
from annotated_logger.mocks import AnnotatedLogMock
from example.api import ApiClient
from example.calculator import BoomError, Calculator, annotated_logger
from example.calculator import BoomError, Calculator, ann_logger
from example.default import DefaultExample, var_args_and_kwargs_provided_outer


Expand Down Expand Up @@ -209,8 +209,8 @@ def test_debug(self, annotated_logger_mock):
def test_runtime_not_cached(self, annotated_logger_mock, mocker):
runtime_mock = mocker.Mock(name="runtime_not_cached")
runtime_mock.side_effect = ["first", "second", "third", "fourth"]
runtime_annotations = annotated_logger.runtime_annotations
annotated_logger.runtime_annotations = {"runtime": runtime_mock}
runtime_annotations = ann_logger.runtime_annotations
ann_logger.runtime_annotations = {"runtime": runtime_mock}
calc = Calculator(12, 13)
calc.subtract()
annotated_logger_mock.assert_logged(
Expand All @@ -231,7 +231,7 @@ def test_runtime_not_cached(self, annotated_logger_mock, mocker):
"runtime": "second",
},
)
annotated_logger.runtime_annotations = runtime_annotations
ann_logger.runtime_annotations = runtime_annotations

def test_raises_type_error_with_too_few_args(self):
calc = Calculator(12, 13)
Expand Down Expand Up @@ -364,7 +364,7 @@ class Weird:
def __len__(self):
return 999

@annotated_logger.annotate_logs(_typing_self=False)
@ann_logger.annotate_logs(_typing_self=False)
def test_me():
return Weird()

Expand Down
Loading