Skip to content

Fix: logging on py310#2802

Merged
winglian merged 3 commits into
mainfrom
fix/py310
Jun 18, 2025
Merged

Fix: logging on py310#2802
winglian merged 3 commits into
mainfrom
fix/py310

Conversation

@NanoCode012

@NanoCode012 NanoCode012 commented Jun 18, 2025

Copy link
Copy Markdown
Collaborator

Description

Reported on Discord by Caitlyn and artem , the logging.logging.getLevelNamesMapping was added in 311 https://docs.python.org/3/library/logging.html#logging.getLevelNamesMapping

This PR adds a try catch to handle for Py310. However, we would now recommend Py311 as that's where we test

Motivation and Context

How has this been tested?

Screenshots (if appropriate)

Types of changes

Social Handles (Optional)

Summary by CodeRabbit

  • Documentation

    • Updated installation instructions to require Python ≥3.11 (previously ≥3.10).
  • Bug Fixes

    • Improved logging configuration to ensure compatibility with both Python 3.11+ and earlier Python versions.

@coderabbitai

coderabbitai Bot commented Jun 18, 2025

Copy link
Copy Markdown
Contributor

"""

Walkthrough

The documentation was updated to require Python ≥3.11 instead of ≥3.10. In the code, the logging filter's initialization was modified to ensure compatibility with Python versions before 3.11 by adding a fallback mechanism for resolving log levels if certain logging APIs are unavailable.

Changes

File(s) Change Summary
docs/installation.qmd Updated minimum Python version requirement from ≥3.10 to ≥3.11 in requirements and instructions.
src/axolotl/logging_config.py Modified log level resolution in filter initialization for compatibility with Python <3.11.

Possibly related PRs

Suggested labels

hold

Poem

A hop and a skip, the docs now say,
Python three-eleven is the only way!
Logging tweaks for versions old,
Ensuring Axolotl’s logs are bold.
With backward steps and forward leaps,
This rabbit’s code now smoothly keeps! 🐇✨
"""


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between dba1646 and 819378d.

📒 Files selected for processing (1)
  • src/axolotl/logging_config.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/axolotl/logging_config.py
⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: PyTest from Source Dist (3.11, 2.5.1)
  • GitHub Check: PyTest (3.11, 2.5.1)
  • GitHub Check: PyTest (3.11, 2.7.1)
  • GitHub Check: PyTest (3.11, 2.6.0)
  • GitHub Check: PyTest from Source Dist (3.11, 2.7.1)
  • GitHub Check: PyTest from Source Dist (3.11, 2.6.0)
  • GitHub Check: pre-commit
  • GitHub Check: pre-commit
  • GitHub Check: preview
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions

github-actions Bot commented Jun 18, 2025

Copy link
Copy Markdown
Contributor

@github-actions github-actions Bot temporarily deployed to preview June 18, 2025 00:08 Inactive

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between da8f6c3 and dba1646.

📒 Files selected for processing (2)
  • docs/installation.qmd (2 hunks)
  • src/axolotl/logging_config.py (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: PyTest (3.11, 2.7.1)
  • GitHub Check: PyTest from Source Dist (3.11, 2.5.1)
  • GitHub Check: PyTest from Source Dist (3.11, 2.7.1)
  • GitHub Check: PyTest (3.11, 2.6.0)
  • GitHub Check: PyTest from Source Dist (3.11, 2.6.0)
  • GitHub Check: pre-commit
  • GitHub Check: PyTest (3.11, 2.5.1)
  • GitHub Check: preview
  • GitHub Check: pre-commit
🔇 Additional comments (1)
docs/installation.qmd (1)

17-17: Python ≥ 3 .11 requirement conflicts with new 3 .10 compatibility fallback

The code change in src/axolotl/logging_config.py purposefully adds support for Python 3.10 by falling back when getLevelNamesMapping is absent, yet the documentation now raises the minimum version to 3.11.
Either remove the fallback and truly drop 3.10, or keep the previous “≥ 3.10 (3.11 recommended)” wording so users aren’t mis-led.

Also applies to: 156-156

Comment thread src/axolotl/logging_config.py Outdated
Comment on lines 28 to 40
axolotl_log_level = os.getenv("AXOLOTL_LOG_LEVEL", DEFAULT_AXOLOTL_LOG_LEVEL)
other_log_level = os.getenv("LOG_LEVEL", DEFAULT_LOG_LEVEL)

try:
# py311+ only
level_mapping = logging.getLevelNamesMapping()
self.axolotl_level = level_mapping[axolotl_log_level]
self.other_level = level_mapping[other_log_level]
except AttributeError:
# For py311-, use getLevelName directly
self.axolotl_level = logging.getLevelName(axolotl_log_level)
self.other_level = logging.getLevelName(other_log_level)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Harden log-level resolution & avoid silent KeyError / case issues

  1. logging.getLevelNamesMapping() will raise KeyError if the env value isn’t an exact key (e.g. “info” or custom levels).
  2. In the fallback branch, logging.getLevelName() returns a string like "Level foo" for unknown names, which will make the later >= comparison raise TypeError.
  3. Conversion should be case-insensitive.

Proposed fix:

-        axolotl_log_level = os.getenv("AXOLOTL_LOG_LEVEL", DEFAULT_AXOLOTL_LOG_LEVEL)
-        other_log_level = os.getenv("LOG_LEVEL", DEFAULT_LOG_LEVEL)
+        axolotl_log_level = os.getenv("AXOLOTL_LOG_LEVEL",
+                                      DEFAULT_AXOLOTL_LOG_LEVEL).upper()
+        other_log_level = os.getenv("LOG_LEVEL",
+                                    DEFAULT_LOG_LEVEL).upper()

         try:
             # py311+ only
             level_mapping = logging.getLevelNamesMapping()
-            self.axolotl_level = level_mapping[axolotl_log_level]
-            self.other_level = level_mapping[other_log_level]
-        except AttributeError:
-            # For py311-, use getLevelName directly
-            self.axolotl_level = logging.getLevelName(axolotl_log_level)
-            self.other_level = logging.getLevelName(other_log_level)
+            self.axolotl_level = level_mapping.get(axolotl_log_level)
+            self.other_level = level_mapping.get(other_log_level)
+            if self.axolotl_level is None or self.other_level is None:
+                raise KeyError
+        except (AttributeError, KeyError):
+            # Fallback for py311- or unknown names
+            self.axolotl_level = logging.getLevelName(axolotl_log_level)
+            self.other_level = logging.getLevelName(other_log_level)
+            if not isinstance(self.axolotl_level, int) or not isinstance(self.other_level, int):
+                raise ValueError(
+                    f"Invalid log level(s): {axolotl_log_level}, {other_log_level}"
+                )

This makes the logic robust across Python versions and user input.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
axolotl_log_level = os.getenv("AXOLOTL_LOG_LEVEL", DEFAULT_AXOLOTL_LOG_LEVEL)
other_log_level = os.getenv("LOG_LEVEL", DEFAULT_LOG_LEVEL)
try:
# py311+ only
level_mapping = logging.getLevelNamesMapping()
self.axolotl_level = level_mapping[axolotl_log_level]
self.other_level = level_mapping[other_log_level]
except AttributeError:
# For py311-, use getLevelName directly
self.axolotl_level = logging.getLevelName(axolotl_log_level)
self.other_level = logging.getLevelName(other_log_level)
axolotl_log_level = os.getenv("AXOLOTL_LOG_LEVEL",
DEFAULT_AXOLOTL_LOG_LEVEL).upper()
other_log_level = os.getenv("LOG_LEVEL",
DEFAULT_LOG_LEVEL).upper()
try:
# py311+ only
level_mapping = logging.getLevelNamesMapping()
self.axolotl_level = level_mapping.get(axolotl_log_level)
self.other_level = level_mapping.get(other_log_level)
if self.axolotl_level is None or self.other_level is None:
raise KeyError
except (AttributeError, KeyError):
# Fallback for py311- or unknown names
self.axolotl_level = logging.getLevelName(axolotl_log_level)
self.other_level = logging.getLevelName(other_log_level)
if not isinstance(self.axolotl_level, int) or not isinstance(self.other_level, int):
raise ValueError(
f"Invalid log level(s): {axolotl_log_level}, {other_log_level}"
)
🤖 Prompt for AI Agents
In src/axolotl/logging_config.py around lines 28 to 40, the current log-level
resolution can raise KeyError if environment variables have unexpected casing or
unknown values, and fallback returns strings causing TypeErrors later. To fix
this, normalize the environment variable values to uppercase before lookup, use
a safe method to get numeric log levels that returns a default or None for
unknown levels, and handle these cases explicitly to avoid exceptions. This
ensures robust, case-insensitive log level parsing across Python versions.

@codecov

codecov Bot commented Jun 18, 2025

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 66.66667% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/axolotl/logging_config.py 66.66% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions github-actions Bot temporarily deployed to preview June 18, 2025 00:55 Inactive
@winglian winglian merged commit 0bb9077 into main Jun 18, 2025
14 of 16 checks passed
@winglian winglian deleted the fix/py310 branch June 18, 2025 19:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants