Skip to content

Explicit Variables Warning Issue #18927

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

Open
Lyranile opened this issue Apr 15, 2025 · 5 comments
Open

Explicit Variables Warning Issue #18927

Lyranile opened this issue Apr 15, 2025 · 5 comments
Labels
bug mypy got something wrong

Comments

@Lyranile
Copy link

Describe the bug
Coverage shows me explicit warning when being used with mypy on this:
console: Console = Console()

I don't see the problem with having an explicit type set
Why does it warn against this, can I disable it globally

Also, doing console = Console() doesn't seem to solve the problem as well

To Reproduce
How can we reproduce the problem? Please be specific. Don't link to a failing CI job. Answer the questions be

Expected behavior
A clear and concise description of what you expected to happen.

Additional context
Add any other context about the problem here.
Python 3.13
Mypy and Coverage
mypy src --html-report test
(Or any report)
Same result when I do coverage run mypy src

For reference I've opened a similar issue in the pycoverage repository and they told me it might be a mypy thing

nedbat/coveragepy#1947

@Lyranile Lyranile added the bug mypy got something wrong label Apr 15, 2025
@brianschubert
Copy link
Collaborator

Hi! What warning are you seeing? Can you provide a self-contained example that reproduces the problem?

Same result when I do coverage run mypy src

As mentioned in the other issue, coverage and mypy are unrelated tools. If both tools are raising issues for the same code, then it's likely that there's an issue with the code that's preventing it from being analyzed as you expect. There isn't enough information here to tell what that may be.

@brianschubert brianschubert added the pending Issues that may be closed label Apr 15, 2025
@Lyranile
Copy link
Author

Let me try to explain myself better, this is relevant to both issues I opened

image.png

Heres the result of the mypy coverage for all of my changes, this shows only the percentage of how much is covered

mypy --cobertura-xml-report .
diff-cover mypy --compare-branch origin/master

I need to see exactly why this isn't 100%, and this is regarding the report

The problem is mainly that I only see issues with the --report flag
mypy .
Wouldn't show any issues while --report show missing lines

The best I could've gotten to show the exact issues of these lines is with --html-report, because inspecting the element show the exact problem

TLDR
image-1.png
I found this console.print being a problem of "explicit any type"

And I'm guessing it's because of this
image-2.png
Having the Console type mentioned, but I need you to help me validate that

What do I expect?
Mypy should show me the exact issues such as when console is having an explicit type

The CLI should have a way to get a detailed view these issues and what mypy thinks is the problem (here I've just stumbled upon inspection that developer tools show me the issue clearer)

Or, the command mypy src to show problems that mypy src --html-report or --xml-report or any report shows
image-3.png
image-1.png

Let me know if I'm not understanding it correctly or if there's something like this

@A5rocks
Copy link
Collaborator

A5rocks commented Apr 19, 2025

You should be able to hover over the line to see the title attribute. In addition, you can use reveal_type to check the type. I haven't looked more at this so I don't know what the issue could be.

@sterliakov
Copy link
Collaborator

sterliakov commented Apr 19, 2025

I usually don't do this much of digging into issues lacking so many details, but let's try.

First of all, please don't post images of code or, if you do, accompany them with a copy-able text of your actual code.

Now, either this issue is valid or mypy works as intended - I'm not familiar with the specific design decisions of any coverage reports. Here's a repro (also works with just def fn(object: Any) -> None, but I'm feeling too lazy to update all reports below):

from typing import Any

def fn(*objects: Any) -> None: 
    print(*objects, sep='\n')

fn("")

Running

mypy foo.py --html-report ./html

Produces a report with two lines annotated as having an explicit Any - def fn (obviously expected, there's an Any right there) and fn(""). The latter is questionable: you call a function accepting Any, does it mean Any appears on that line? Probably yes, as that report is about any possible interaction with Any types, as those are rather contagious. But this may also be surprising for users. At least it should be of yellow (warning) color and not red.

Report screenshot

Or, as a probably more readable, the following XML report (formatted by me, from mypy foo.py --xml-report ./xml && cat ./xml/xml/foo.py.xml):

<?xml-stylesheet type="text/xsl" href="../mypy-html.xslt"?>
<mypy-report-file name="foo.py" module="foo">
    <line any_info="No Anys on this line!"
          content="from typing import Any"
          number="1"
          precision="precise" />
    <line any_info="No Anys on this line!" content="" number="2" precision="empty" />
    <line any_info="Any Types on this line: &#10;Explicit (x1)"
          content="def fn(*objects: Any) -&gt; None: "
          number="3"
          precision="any" />
    <line any_info="Any Types on this line: &#10;Explicit (x1)"
          content="    print(*objects, sep='\n')"
          number="4"
          precision="imprecise" />
    <line any_info="No Anys on this line!" content="" number="5" precision="empty" />
    <line any_info="Any Types on this line: &#10;Explicit (x1)"
          content="fn(&quot;&quot;)"
          number="6"
          precision="any" />
    <line any_info="No Anys on this line!" content="" number="7" precision="empty" />
</mypy-report-file>

It's a repro because

https://github.com/Textualize/rich/blob/0c6c75644f80530de219dae3e94f0aeb999f9b4c/rich/console.py#L1643-L1660

@sterliakov sterliakov removed the pending Issues that may be closed label Apr 19, 2025
@Lyranile
Copy link
Author

Sorry for being unclear, yes your reproduction seems to be what I faced
Any updates about this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

4 participants