Comments (14)
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.
@ThibaultLemaire released @ https://pypi.org/project/loguru-mypy/0.0.3
from loguru-mypy.
(IMHO this is more a bug since it's flagging valid syntax)
from loguru-mypy.
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.
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.
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.
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.
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.
There we go, I've got some sprout of working code, continuing technical discussion over at PR #43
from loguru-mypy.
Hey @kornicameister when do you think this will be released? I'd like to use it at work.
from loguru-mypy.
@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.
Ah that's too bad, don't worry though, this is low priority for me.
from loguru-mypy.
Not for me ;-)
I have some time off to play around here, so I will release things soon I believe.
from loguru-mypy.
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)
- Combine coverage and upload to external service. HOT 1
- Document project HOT 2
- Figure out possibility of tracking logger objects HOT 1
- Add catching of `record=True` HOT 4
- Draft first release HOT 2
- Ideas for further checks HOT 6
- Add this to awesome-python-typing HOT 2
- Test against 3 latest releases of mypy + hard pin in plugin hook na latest HOT 2
- Embed logger options in returned type HOT 4
- "Internal error" while logging non-string messages HOT 4
- Using `mypy` to verify `check_str_format_call`? HOT 2
- Implement handling of `OpExpr`
- Implement handling of `CallExpr`
- False positive on `extra` kwargs HOT 3
- Releases are not working HOT 1
- Cannot import loguru.Writable HOT 6
- Support new mypy 1.0 version specifiers
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from loguru-mypy.