GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Update logging LogRecord msg attribute by aminalaee · Pull Request #9914 · python/typeshed

Conversation

@aminalaee

@aminalaee aminalaee commented Mar 21, 2023

Copy link
Copy Markdown
Contributor

Closes #9913

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@aminalaee
Update logging LogRecord msg argument
c0af7af
@github-actions

This comment has been minimized.

Sign in to view
@AlexWaygood

AlexWaygood commented Mar 21, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

It looks like this would be a partial revert of #1776 by @rowillia.

Since there are no mypy-primer hits, it seems unlikely that too many people are passing non-str objects in this specific situation. And the inconsistency with the attribute annotation is indeed incorrect -- they should either both be str or both be object.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood

AlexWaygood commented Mar 21, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

But I'm not sure it makes sense to only do a partial revert of #1776. msg annotations should probably be the same across all of logging/__init__.pyi. For example, logging.Logger.debug currently uses object as the annotation for the msg parameter:

typeshed/stdlib/logging/__init__.pyi

Lines 115 to 117 in dd2818a

def debug(
self,
msg: object,

But logging.Logger.debug calls logging.Logger._log, which passes msg to logging._logRecordFactory, and logging._logRecordFactory is an alias for logging.LogRecord. So if you passed a non-str object to logging.Logger.debug, you'd end up passing a non-str object to the logging.LogRecord constructor. Meaning that the annotation for the msg parameter for logging.Logger.debug (and logging.Logger.{info,warning,warn,error,exception,critical,log,_log}) should be kept in sync with the annotation for the msg parameter of logging.LogRecord.__init__.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood

AlexWaygood commented Mar 21, 2023

Copy link
Copy Markdown
Member

In the issue, you wrote:

According to the Python docs for the LogRecord in logging module the msg argument and property is a string:
https://docs.python.org/3/library/logging.html#logging.LogRecord

However, the CPython docs are pretty inconsistent here: later on down in the docs, it's specifically stated that it's okay to pass non-str objects in as messages:

In the preceding sections and examples, it has been assumed that the message passed when logging the event is a string. However, this is not the only possibility. You can pass an arbitrary object as a message, and its __str__() method will be called when the logging system needs to convert it to a string representation. In fact, if you want to, you can avoid computing a string representation altogether - for example, the SocketHandler emits an event by pickling it and sending it over the wire.

This seems to me like a bug in the CPython docs: they shouldn't say one thing up at the top, only to say a different thing later down.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@aminalaee

aminalaee commented Mar 21, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

I hadn't really checked the other places, you're right.
This is much deeper in the docs than the CPython and typeshed. I will create another PR on CPython docs.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

AlexWaygood
AlexWaygood requested changes Mar 21, 2023
View reviewed changes

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member

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.

Since the CPython docs specifically say that it's okay to pass in arbitrary non-str objects to the msg parameter, I think a better fix would be to keep object for the parameter annotation here, but change this attribute annotation so that it is also object:

typeshed/stdlib/logging/__init__.pyi

Line 392 in dd2818a

msg: str

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@aminalaee
Update __init__.pyi
bf770a7
AlexWaygood
AlexWaygood approved these changes Mar 21, 2023
View reviewed changes

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member

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.

Thank you! A docs PR over at CPython would also be great. If you ping me on the PR, I can help review it :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

aminalaee reacted with heart emoji
All reactions
  • ❤️ 1 reaction
@aminalaee aminalaee changed the title Update logging LogRecord msg argument Update logging LogRecord msg attribute Mar 21, 2023
@github-actions

This comment has been minimized.

Sign in to view
AlexWaygood
AlexWaygood reviewed Mar 21, 2023
View reviewed changes
Comment thread stdlib/logging/__init__.pyi Resolved

Uh oh!

There was an error while loading. Please reload this page.

@aminalaee @AlexWaygood
Update stdlib/logging/__init__.pyi
2cd7b4c
Co-authored-by: Alex Waygood <[email protected]>
@github-actions

github-actions Bot commented Mar 21, 2023

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

AlexWaygood and aminalaee reacted with hooray emoji
All reactions
  • 🎉 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood
AlexWaygood merged commit b6564d9 into python:main Mar 21, 2023
@aminalaee
aminalaee deleted the patch-1 branch March 21, 2023 15:44
@aminalaee aminalaee mentioned this pull request Mar 21, 2023
logging module LogRecord can accept non-str msg but it's not documented python/cpython#102873
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@AlexWaygood AlexWaygood AlexWaygood approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

logging module LogRecord __init__ mismatch

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@aminalaee @AlexWaygood
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.