-
Notifications
You must be signed in to change notification settings - Fork 58
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
Replacing "print" with "logging" Module #974
Conversation
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good work @merrickliu888, here is some feedback on the failing tests.
- The first set of tests (in
test_messages_config.py
) are failing because they are checking whether specific error messages are being printed tostdout
, which I suppose isn't happening now with the logging module. The easiest fix to this is to change thesubprocess.run
call to apython_ta.check_all
call, with the appropriate arguments (the test file and the configuration file). Then, use the same pytest fixture to capture the logging as you've done with the other tests. - You're likely correct about the monkey-patching for the
sys.version_info
interfering with other test cases. Doing a test teardown that again reloads thepython_ta
module (with the monkey patching disabled) is a great idea. - As for the failing GitHub CI tests, this is a new problem with z3 (Python bindings: Missing
importlib_resources
dependency Z3Prover/z3#7041). You can fix this for now by modifying thepyproject.toml
file'sz3-solver
dependency to bez3-solver==4.12.1.0
. (This is temporary---I'll make a PR to fix this separately, but I don't want it to hold up your PR.)
@david-yz-liu I did some investigation for the tests that were failing in |
for more information, see https://pre-commit.ci
Pull Request Test Coverage Report for Build 7161535366
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@merrickliu888 great work! I left some fairly minor comments here
python_ta/__init__.py
Outdated
@@ -53,11 +54,14 @@ | |||
from .reporters import REPORTERS | |||
from .upload import upload_to_server | |||
|
|||
# Configuring logger |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at this, I realized that we really shouldn't have this top-level code at all.
- Move this to the top of the
_check
method. - Move the
sys.version_info < (3, 7, 0)
part there as well. - I realize this actually makes your
sys.version_info
test a lot simpler...! 😅
python_ta/config/__init__.py
Outdated
@@ -76,7 +76,7 @@ def load_messages_config(path: str, default_path: str, use_pyta_error_messages: | |||
try: | |||
merge_from = toml.load(path) | |||
except FileNotFoundError: | |||
print(f"[WARNING] Could not find messages config file at {str(Path(path).resolve())}.") | |||
logging.warning(f"Could not find messages config file at {str(Path(path).resolve())}. ") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Delete the space at the end of the string
tests/test_config/test_config.py
Outdated
import os | ||
from unittest.mock import mock_open, patch | ||
|
||
import pylint.lint |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You do not need both this and the other pylint
import statement you added. The typical idiom is import pylint.lint as lint
|
||
|
||
def test_contracts_debug(caplog) -> None: | ||
"""Test to see if _debug method is logging messages correctly""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_debug
is a function, but not a method
tests/test_init_logging.py
Outdated
] | ||
|
||
# Iterating through generators to produce errors | ||
[_ for _ in _get_valid_files_to_check(module_name={"examples/nodes/assign"})] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For a generator, you can execute until the first yield
by calling next
, e.g. next(_get_valid_files_to_check(...))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat. I didn't know about that. I also just looked it up, I could also just pass the generator function into tuple()
. In this case, would it be better to use a while
loop and next
, or keep the code minimal and just use the tuple()
constructor and "one-line" it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@merrickliu888 sorry I missed this! Using tuple
was totally fine. :)
- Revert python_ta.doc function to print message to stdout - Move logging config into functions (rather than at top-level) - Update tests and fix Windows encoding issue in test cases
Motivation and Context
"print" only prints out text, while "logging" is able to store meta data with their messages, such as the time and line of code where a "logging" message was outputted. "logging" provides more functionality and extensibility for future changes regarding user interaction with PyTA, and debugging runtime issues.
Your Changes
__init__.py
,configs/
, andcontracts/
.UnicodeDecodeError
in the_verify_pre_check
function in the top level__init__.py
ofpython_ta
. The issue was that when the lines that produced theUnicodeDecodeError
were logged, the line number was one off asenumerate
is 0-indexed, rather than starting at 1. I fixed it by adding 1 toi
.sys.version_info
if statement from being a top level statement in the__init__.py
ofpython_ta
into the_check
function.Type of change (select all that apply):
Testing
I tested these changes by manually triggering the "logging" and creating new unit tests for each "logging" call to verify that the message was logged correctly (message content and logging level ie. "ERROR", "INFO", etc.)
Checklist