pyrefly icon indicating copy to clipboard operation
pyrefly copied to clipboard

Incorrect type error when using `unittest.main()`

Open skybaks opened this issue 6 months ago • 1 comments
trafficstars

Describe the Bug

Pyrefly incorrectly reports an error on the following code:

import unittest
unittest.main()  # ERROR 2:1-14: Expected a callable, got Module[unittest.main]

This is valid python and also one recommended way to use the unittest library as shown in the python docs.

I think what is happening here is that unittest includes this import in its init.py which brings the main object here into context and shadows the module named main.py.

It looks like Pyrefly is not taking into account that shadowing of the from .main import main and assuming that "main" is referring to the module when, in fact, it is not.

Sandbox Link

https://pyrefly.org/sandbox/?code=JYWwDg9gTgLgBAVwHbBjApgZxgKGajbAOhAENgkAKASiA

(Only applicable for extension issues) IDE Information

No response

skybaks avatar May 20 '25 23:05 skybaks

Thanks for the bug report! I think your diagnosis is correct.

We recently (within the past few weeks) updated Pyrefly to model side effects of imports in __init__.py modules; those side effects mean that if we had run from .main import foo, then import unittest; unittest.main would indeed result in the main module.

But that behavior only happens when there's no actual main name defined directly in the __init__.py module, because only in that case does Python fall back to the import system side-effects; in this case it does not.

stroxler avatar May 22 '25 02:05 stroxler