Code Monkey home page Code Monkey logo

Comments (14)

issue-label-bot avatar issue-label-bot commented on August 19, 2024 1

Issue-Label Bot is automatically applying the label feature_request to this issue, with a confidence of 0.80. Please mark this comment with 👍 or 👎 to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

from loguru-mypy.

kornicameister avatar kornicameister commented on August 19, 2024 1

@ThibaultLemaire released @ https://pypi.org/project/loguru-mypy/0.0.3

PyPI

from loguru-mypy.

ThibaultLemaire avatar ThibaultLemaire commented on August 19, 2024

(IMHO this is more a bug since it's flagging valid syntax)

from loguru-mypy.

kornicameister avatar kornicameister commented on August 19, 2024

HI @ThibaultLemaire . I am deeply sorry but I don't think there will be much time for me to take a look. Actually I don't have time even to finish PR that is already here and another code sits in my desk. Dang, it was even 7 days already since reporting this issue.

If you have an idea how to fix that PR are welcome ;)

PS. It actually looks ok, so I think you're right and that's a bug.

If you don't mind dropping loguru version, that'd be great.

from loguru-mypy.

ThibaultLemaire avatar ThibaultLemaire commented on August 19, 2024

I'll take a look at the code and see what I can do. It should be the opportunity to see how a mypy plugin is made, maybe I'll learn a trick or two to enhance mypy checks (I'm thinking mainly of type-checking pymongo/motor queries, as that's something I've been meaning to do for a long time).

I've updated the description with loguru and loguru-mypy versions

from loguru-mypy.

ThibaultLemaire avatar ThibaultLemaire commented on August 19, 2024

Okay, so it looks like you've got some custom code there to support loguru's syntax, but mypy already has the capability to type .format(), like so:

class Foo:
    bar = "baz"

foo = Foo()

'The bar is "{0.bar}"'.format(foo)  # typechecks
'The bar is "{0.bar}"'.format("not foo")  # error: "str" has no attribute "bar"
'The bar is "{my_foo.bar}"'.format(my_foo=foo)  # typechecks
'The bar is "{my_foo.bar}"'.format(my_foo="not foo")  # error: "str" has no attribute "bar"

So I wonder if we could leverage mypy directly to typecheck something like

logger.debug('The bar is "{my_foo.bar}"', my_foo=foo)

as if it was written like this

logger.debug('The bar is "{my_foo.bar}"'.format(my_foo=foo))

from loguru-mypy.

ThibaultLemaire avatar ThibaultLemaire commented on August 19, 2024

Progress report

After wiring up a debugger to mypy I was able to analyse the way it checks calls to .format. The call stack looks roughly like (Most recent call last):

mypy.checkexpr.ExpressionChecker.visit_call_expr
mypy.checkexpr.ExpressionChecker.check_str_format_call
mypy.checkstrformat.StringFormatterChecker.check_str_format_call

Where that last check_str_format_call is the one I'm really interested in, the one that should do all the type checking for us.

Now all I need is a way to call that method with all the necessary arguments as mypy is a very tightly coupled mess.

Hopefully though, the ctx.api that is passed to the plugin seems to have quite a lot of methods available, among which is visit_call_expr, so that looks like a promising line of investigation

from loguru-mypy.

ThibaultLemaire avatar ThibaultLemaire commented on August 19, 2024

Progress report

So after further investigation, ctx.api is an instance of mypy.checker.TypeChecker that's why it has all those nice methods.

Now, I've been able to create a mypy.nodes.CallExpr that looks enough like that of a .format call (I think) and I was able to call ctx.api.visit_call_expr without error, but I don't get the corresponding mypy warnings...

I'll have to trace and debug mypy with the loguru plugin to see exactly what's going wrong.

from loguru-mypy.

ThibaultLemaire avatar ThibaultLemaire commented on August 19, 2024

There we go, I've got some sprout of working code, continuing technical discussion over at PR #43

from loguru-mypy.

ThibaultLemaire avatar ThibaultLemaire commented on August 19, 2024

Hey @kornicameister when do you think this will be released? I'd like to use it at work.

from loguru-mypy.

kornicameister avatar kornicameister commented on August 19, 2024

@ThibaultLemaire I will release it with 0.0.3 but there are some build errors.I think I have introduced some time ago.

from loguru-mypy.

ThibaultLemaire avatar ThibaultLemaire commented on August 19, 2024

Ah that's too bad, don't worry though, this is low priority for me.

from loguru-mypy.

kornicameister avatar kornicameister commented on August 19, 2024

Not for me ;-)
I have some time off to play around here, so I will release things soon I believe.

from loguru-mypy.

ThibaultLemaire avatar ThibaultLemaire commented on August 19, 2024

Thank you very much, I added loguru-mypy to our project on a feature branch and confirmed it's working in the scenario that initally prompted me to open this issue. 👍

However, I'm hitting two new scenarii where the plugin fails to correctly validate the code 🙁

The first, I had anticipated when working on my PR:

from loguru import logger

logger.configure(
    handlers=[
        dict(
            sink=sys.stderr,
            format="Site {extra[site]} broadcasting: {message}",
            backtrace=False,
            diagnose=False,
        ),
    ],
    extra={"site": "unknown"},
)

site = 19
scp = 682
# We're passing the `site` number for local formatting of this message, but we're also passing the `scp` number as `extra` for the formatter of the handler.
logger.warning("SCP-{} containment breach detected", scp, site=site)
# error: Not all arguments converted during string formatting
# But this is easily circumvented:
logger.bind(site=site).warning("SCP-{} containment breach detected", scp)

The workaround isn't too bad, and fixing the issue - I think - would prove a tad more complicated than my previous PR (see #48 ).

But the second issue is a real blocker: essentially it's the 3rd exemple of #49 as reported by @Delgan

from loguru import logger

foo = "bar"
logger.info(foo)  # error: INTERNAL ERROR

So I think I'm going to start working on fix for that, as I think it shouldn't be too complicated.

from loguru-mypy.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.