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

Suppresses griffe logs #7059

Merged
merged 5 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
11 changes: 5 additions & 6 deletions src/prefect/blocks/core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import hashlib
import inspect
import logging
import sys
import warnings
from abc import ABC
Expand Down Expand Up @@ -28,6 +27,7 @@
import prefect
from prefect.client.orion import inject_client
from prefect.exceptions import PrefectHTTPStatusError
from prefect.logging.loggers import disable_logger
from prefect.orion.schemas.core import (
DEFAULT_BLOCK_SCHEMA_VERSION,
BlockDocument,
Expand Down Expand Up @@ -409,11 +409,10 @@ def _parse_docstring(cls) -> List[DocstringSection]:
`<module>:11: No type or annotation for parameter 'write_json'`
because griffe is unable to parse the types from pydantic.BaseModel.
"""
griffe_logger = logging.getLogger("griffe.docstrings.google")
griffe_logger.disabled = True
docstring = Docstring(cls.__doc__)
parsed = parse(docstring, Parser.google)
griffe_logger.disabled = False
with disable_logger("griffe.docstrings.google"):
with disable_logger("griffe.agents.nodes"):
Comment on lines +412 to +413
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wanted to put this in one line, but too long on one line and it didn't like a parentheses:
d4c3955

docstring = Docstring(cls.__doc__)
parsed = parse(docstring, Parser.google)
return parsed

@classmethod
Expand Down
33 changes: 23 additions & 10 deletions src/prefect/logging/loggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,31 @@ def task_run_logger(


@contextmanager
def disable_run_logger():
flow_run_logger = get_logger("prefect.flow_run")
task_run_logger = get_logger("prefect.task_run")
def disable_logger(name: str):
"""
Get a logger by name and disables it within the context manager.
Upon exiting the context manager, the logger is returned to its
original state.
"""
logger = logging.getLogger(name=name)

# determine if it's already disabled
flow_run_logger_disabled = flow_run_logger.disabled
task_run_logger_disabled = task_run_logger.disabled
base_state = logger.disabled
try:
flow_run_logger.disabled = True
task_run_logger.disabled = True
# disable the logger
logger.disabled = True
yield
finally:
# return to original state
flow_run_logger.disabled = flow_run_logger_disabled
task_run_logger.disabled = task_run_logger_disabled
# return to base state
logger.disabled = base_state


@contextmanager
def disable_run_logger():
"""
Gets both `prefect.flow_run` and `prefect.task_run` and disables them
within the context manager. Upon exiting the context manager, both loggers
are returned to its original state.
"""
with disable_logger("prefect.flow_run"), disable_logger("prefect.task_run"):
yield
12 changes: 12 additions & 0 deletions tests/blocks/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,18 @@ class A(Block):

assert A.get_description() == "But I will"

def test_no_griffe_logs(self, caplog):
ahuang11 marked this conversation as resolved.
Show resolved Hide resolved
class A(Block):
"""
Without disable logger, this spawns griffe warnings.

Args:
string (str): This should spawn a warning
"""

A()
assert caplog.record_tuples == []


class NoCodeExample(Block):
_block_type_name = "No code Example"
Expand Down
33 changes: 33 additions & 0 deletions tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
)
from prefect.logging.handlers import OrionHandler, OrionLogWorker
from prefect.logging.loggers import (
disable_logger,
disable_run_logger,
flow_run_logger,
get_logger,
Expand Down Expand Up @@ -1047,6 +1048,38 @@ def test_flow():
}


def test_without_disable_logger(caplog):
"""
Sanity test to double check whether caplog actually works
so can be more confident in the asserts in test_disable_logger.
"""
logger = logging.getLogger("griffe.agents.nodes")

def function_with_logging(logger):
assert not logger.disabled
logger.critical("it's enabled!")
return 42

function_with_logging(logger)
assert not logger.disabled
assert caplog.record_tuples == [("griffe.agents.nodes", 50, "it's enabled!")]


def test_disable_logger(caplog):
logger = logging.getLogger("griffe.agents.nodes")

def function_with_logging(logger):
logger.critical("I know this is critical, but it's disabled!")
return 42

with disable_logger(logger.name):
assert logger.disabled
function_with_logging(logger)

assert not logger.disabled
assert caplog.record_tuples == []


def test_disable_run_logger(caplog):
@task
def task_with_run_logger():
Expand Down