cloudtools / troposphere

troposphere - Python library to create AWS CloudFormation descriptions
BSD 2-Clause "Simplified" License
4.93k stars 1.45k forks source link

Upgrading `4.3.2` -> `4.4.1` causes mypy type checking to fail #2186

Open rakunoi opened 1 year ago

rakunoi commented 1 year ago

Hi,

to keep it short, when upgraded 4.3.2 -> 4.4.1, mypy started throwing following errors

Skipping analyzing "troposphere": module is installed, but missing library stubs
or py.typed marker  [import]
    from troposphere import Ref
    ^
Skipping analyzing "troposphere.ecs": module is installed, but missing library
stubs or py.typed marker  [import]
    from troposphere.ecs import Environment as EnvironmentVariable
    ^
Skipping analyzing "troposphere": module is installed, but missing library stubs
or py.typed marker  [import]
    from troposphere import (
    ^
Skipping analyzing "troposphere.applicationautoscaling": module is installed,
but missing library stubs or py.typed marker  [import]
    from troposphere.applicationautoscaling import (
    ^

Skipping analyzing "troposphere.cloudwatch": module is installed, but missing
library stubs or py.typed marker  [import]
    from troposphere.cloudwatch import Alarm, MetricDimension
    ^

Skipping analyzing "troposphere.ec2": module is installed, but missing library
stubs or py.typed marker  [import]
    from troposphere.ec2 import SecurityGroup, SecurityGroupEgress, Securi...
    ^

Skipping analyzing "troposphere.ecs": module is installed, but missing library
stubs or py.typed marker  [import]
    from troposphere.ecs import (
    ^

Skipping analyzing "troposphere.efs": module is installed, but missing library
stubs or py.typed marker  [import]
    from troposphere.efs import (
    ^

Skipping analyzing "troposphere.elasticloadbalancingv2": module is installed,
but missing library stubs or py.typed marker  [import]
    from troposphere.elasticloadbalancingv2 import (
    ^

mypy config is


[tool.mypy]
python_version = "3.11"
strict = true
pretty = true
show_error_codes = true
warn_unused_ignores = false
plugins = ["pydantic.mypy"]

Pydantic version is "==1.10.12" Mypy version is mypy = "1.5.1"

Please let me know if more information is needed.

markpeek commented 1 year ago

There was an issue with py.typed being included in Release 4.3.2 which was fixed. This is related to this earlier issue describing why it should not be included at this time. At this time exclude troposphere from the mypy checks.

g-borgulya commented 10 months ago

It's probably a mypy related question, but rather bring it up here, as this whole mypy "which modules to ignore and how" is pretty confusing (seemingly to many).

I've tried to add both below to the mypy.ini, but neither of them helped:

[mypy-troposhpere.*]
ignore_missing_imports = True

[mypy-troposhpere]
ignore_missing_imports = True

Still getting error: Skipping analyzing "troposphere": module is installed, but missing library stubs or py.typed marker [import-untyped] when running mypy on the selected files.

Putting it to the top level makes running mypy ignore the issue, but that's not ideal as it affects other packages as well:

[mypy]
ignore_missing_imports = True

This results in Success: no issues found in 82 source files.

(It's another issue though that with this setting I'm still getting Stub file not found for "troposphere" from PyLance in VSCode.)

I'd really appreciate any recommendations how to get this working properly, I've spent at least an hour with this specifically because of troposphere.

markpeek commented 10 months ago

@g-borgulya I was able to reproduce with your example. I believe the issue was the spelling of "troposphere" in the section names. Here's my results correcting that issue.

$ cat m.py
import troposphere

print(troposphere.__version__)
t = troposphere.Template()
print(t.to_json())
$ python m.py
4.5.3
{
 "Resources": {}
}
$ cat mypy.ini
[mypy]

[mypy-troposphere.*]
ignore_missing_imports = True

[mypy-troposphere]
ignore_missing_imports = True
$ mypy m.py
Success: no issues found in 1 source file
$

Let me know if this works for you. I do need to create a tracking issue and branch to get better typing support implemented.

g-borgulya commented 9 months ago

I so much appreciate it, @markpeek ! Thank you. Yes, mypy works with the fixed settings.