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

Replacing "print" with "logging" Module #974

Merged
merged 51 commits into from
Dec 11, 2023

Conversation

merrickliu888
Copy link
Contributor

@merrickliu888 merrickliu888 commented Oct 20, 2023

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

  • Replaced all "print" calls with the appropriate "logging" method in __init__.py, configs/, and contracts/.
  • In addition, fixed a small bug regarding the reporting of the UnicodeDecodeError in the _verify_pre_check function in the top level __init__.py of python_ta. The issue was that when the lines that produced the UnicodeDecodeError were logged, the line number was one off as enumerate is 0-indexed, rather than starting at 1. I fixed it by adding 1 to i.
    image
  • Moved the sys.version_info if statement from being a top level statement in the __init__.py of python_ta into the _check function.

Type of change (select all that apply):

  • Refactoring (internal change to codebase, without changing functionality)
  • Test update (change that modifies or updates tests only)

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

  • I have performed a self-review of my own code.
  • I have verified that the CI tests have passed.
  • I have reviewed the test coverage changes reported on Coveralls.
  • I have added tests for my changes.

@merrickliu888 merrickliu888 changed the title Replacing "print" with "logging" Module Replacing "print" with "logging" Module (Draft) Oct 20, 2023
@merrickliu888 merrickliu888 marked this pull request as draft November 15, 2023 19:20
@merrickliu888 merrickliu888 marked this pull request as ready for review November 28, 2023 17:52
Copy link
Contributor

@david-yz-liu david-yz-liu left a 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.

  1. The first set of tests (in test_messages_config.py) are failing because they are checking whether specific error messages are being printed to stdout, which I suppose isn't happening now with the logging module. The easiest fix to this is to change the subprocess.run call to a python_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.
  2. 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 the python_ta module (with the monkey patching disabled) is a great idea.
  3. 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 the pyproject.toml file's z3-solver dependency to be z3-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.)

@merrickliu888
Copy link
Contributor Author

merrickliu888 commented Dec 6, 2023

@david-yz-liu I did some investigation for the tests that were failing in test_messages_config.py and it seems the reason its failing is because of the path to the files they need to access. At least locally. I need to see if this error happens in GitHub.

@coveralls
Copy link
Collaborator

coveralls commented Dec 6, 2023

Pull Request Test Coverage Report for Build 7161535366

  • 29 of 29 (100.0%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.2%) to 94.837%

Totals Coverage Status
Change from base Build 7160350456: 0.2%
Covered Lines: 3453
Relevant Lines: 3641

💛 - Coveralls

Copy link
Contributor

@david-yz-liu david-yz-liu left a 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

@@ -53,11 +54,14 @@
from .reporters import REPORTERS
from .upload import upload_to_server

# Configuring logger
Copy link
Contributor

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...! 😅

@@ -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())}. ")
Copy link
Contributor

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

import os
from unittest.mock import mock_open, patch

import pylint.lint
Copy link
Contributor

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"""
Copy link
Contributor

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

]

# Iterating through generators to produce errors
[_ for _ in _get_valid_files_to_check(module_name={"examples/nodes/assign"})]
Copy link
Contributor

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(...))

Copy link
Contributor Author

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?

Copy link
Contributor

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. :)

@david-yz-liu david-yz-liu changed the title Replacing "print" with "logging" Module (Draft) Replacing "print" with "logging" Module Dec 10, 2023
- 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
@david-yz-liu david-yz-liu merged commit 2ce5460 into pyta-uoft:master Dec 11, 2023
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants