systemd / mkosi

💽 Build Bespoke OS Images
https://mkosi.systemd.io/
1.18k stars 319 forks source link

Mypy warnings #369

Closed behrmann closed 4 years ago

behrmann commented 5 years ago

Running a current mypy on mkosi gives the following warnings

mkosi:968: error: Argument 1 to "partition" has incompatible type "Optional[str]"; expected "str"
mkosi:971: error: Argument 1 to "partition" has incompatible type "Optional[str]"; expected "str"
mkosi:4624: error: "Dict[str, CommandLineArguments]" has no attribute "director

The last line is harmless and addressed in #368. I guess it simply prevents the --directory switch to take effect.

I haven't yet followed through the first error, though. The partition function cannot deal with the None that mount_image says loopdev might be. The only user of mount_image is in build_image which gets the loopdev it passes on from attach_image_loopback, which will produce a None if raw is None. That seems to be the case if the output format is not a disk format. Right now I wonder where to best untangle this. Any ideas?

poettering commented 5 years ago

@keszybz you are our python guru on call, any idea? ;-)

behrmann commented 5 years ago

I've finally had some time to follow the logic around a bit and this a bit of a limitation in mypy. mypy cannot deduce, that loopdev can only be None, when args.esp_partno and args.xbootldr_partno are also None, since loopdev will be None, if the image is not a disk image, but then determine_partition_table forces them to be None as well. At least if I understand the situation correctly.

The simplest solution would be to add and loopdev is not None to both lines in mount_image. I'll prepare a patch once I'm in a less crappy wifi.

behrmann commented 5 years ago

I've just opened #374 with the most straight-forward solution. I hope the reasoning is correct.

I've also rechecked mkosi with mypy --strict, which on my mypy 0.730 sets --warn-unused-configs, --disallow-subclassing-any, --disallow-any-generics, --disallow-untyped-calls, --disallow-untyped-defs, --disallow-incomplete-defs, --check-untyped-defs, --disallow-untyped-decorators, --no-implicit-optional, --warn-redundant-casts, --warn-unused-ignores, --warn-return-any, --no-implicit-reexport, to get a few more errors

mkosi:69: error: Missing type parameters for generic type "CompletedProcess"
mkosi:107: error: Function is missing a return type annotation
mkosi:111: error: Function is missing a return type annotation
mkosi:412: error: unused 'type: ignore' comment
mkosi:432: error: Missing type parameters for generic type "Callable"
mkosi:443: error: Missing type parameters for generic type "TemporaryDirectory"
mkosi:1917: error: Missing type parameters for generic type "CompletedProcess"
mkosi:1926: error: Missing type parameters for generic type "CompletedProcess"
mkosi:3127: error: Missing type parameters for generic type "TemporaryDirectory"
mkosi:3128: error: Missing type parameters for generic type "TemporaryDirectory"
mkosi:3201: error: Function is missing a type annotation
mkosi:3206: error: Function is missing a type annotation
mkosi:3257: error: Function is missing a type annotation
mkosi:3263: error: Name '__class__' is not defined
mkosi:3266: error: Function is missing a type annotation
mkosi:3282: error: Function is missing a type annotation
mkosi:3286: error: Function is missing a type annotation
mkosi:3288: error: Name '__class__' is not defined
mkosi:3290: error: Call to untyped function "camel_to_arg" in typed context
mkosi:3307: error: Call to untyped function "ini_key_to_cli_arg" in typed context
mkosi:3331: error: Call to untyped function "ArgumentParserMkosi" in typed context
mkosi:3408: error: Call to untyped function "doc" of "SourceFileTransfer" in typed context
mkosi:3457: error: Returning Any from function declared to return "ArgumentParserMkosi"
mkosi:3464: error: Function is missing a type annotation for one or more arguments
mkosi:3557: error: Redundant cast to "CommandLineArguments"
mkosi:3561: error: Function is missing a type annotation for one or more arguments
mkosi:3580: error: Redundant cast to "CommandLineArguments"
mkosi:3607: error: Returning Any from function declared to return "Optional[int]"
mkosi:4344: error: Missing type parameters for generic type "TemporaryDirectory"
mkosi:4726: error: Function is missing a type annotation
mkosi:4772: error: Call to untyped function "run_verb" in typed context
Found 31 errors in 1 file (checked 1 source file)

These are addressed in #375.

When they get merged I'd like to add mypy to the tests that are being run. --strict is quite a bit too harsh, but I guess a subset of its options might be helpful. I also need to see whether the current mypy 0.750 introduced anything new. Once this is done I will finally test, whether mypyc could compile this, hopefully in a standalone binary.

behrmann commented 4 years ago

I've reworked the PR on this into #389, which makes mkosi mypy strict compliant (with mypy 0.750, 0.740 for example would throw an error because of a regression mypy, that has since been fixed) and adds mypy to the tests that get run by semaphore.

behrmann commented 4 years ago

I've now gone through all options that mypy has, which was a nice learning experience and decided to add all current strict switches to the mypy section of setup.cfg.

There is one last switch I'd like to switch on unreachable code, that I'd like to switch one, but currently it is triggered and I'm not quite sure if it's a false positve, because that PR also introduced some indentation with tabs. I asked about it in #357 in the hope to reach the original author.