Skip to content

Commit

Permalink
Merge pull request #7059 from PrefectHQ/suppress_griffe
Browse files Browse the repository at this point in the history
Suppresses griffe logs
  • Loading branch information
ahuang11 committed Oct 5, 2022
2 parents 1cfeeec + e01d4e2 commit 1dcd456
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 16 deletions.
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"):
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
31 changes: 31 additions & 0 deletions tests/blocks/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,37 @@ class A(Block):

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

def test_no_griffe_logs(self, caplog, capsys, recwarn):
"""
Ensures there are no extraneous output printed/warned.
"""

class A(Block):
"""
Without disable logger, this spawns griffe warnings.
Args:
string (str): This should spawn a warning
"""

A()
assert caplog.record_tuples == []

captured = capsys.readouterr()
assert captured.out == ""
assert captured.err == ""

assert len(recwarn) == 0

# to be extra sure that we are printing anything
# we shouldn't be
print("Sanity check!")
captured = capsys.readouterr()
assert captured.out == "Sanity check!\n"

warnings.warn("Sanity check two!")
assert len(recwarn) == 1


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

0 comments on commit 1dcd456

Please sign in to comment.