PyFixate / Fixate

Framework for hardware test fixtures and automatic test environments
MIT License
22 stars 17 forks source link

Mypy #154

Closed daniel-montanari closed 1 year ago

daniel-montanari commented 2 years ago

I don't love how I've handled dependencies, possibly another way to go about it is just ignore 3rd party stuff all together, and globally allow for missing imports. This way the only dependency is mypy. I had a look at what we miss out on by not getting type information from other packages, and currently it looks like it actually makes things worse - we get nothing useful out of knowing the types since our own code isn't typed yet, but we still have to deal with suppressing warnings due to runtime import mechanics confusing mypy.

clint-lawrence commented 2 years ago

Me just playing. Didn't think too hard about it, just wanted to see what kind of progress I could make https://github.com/clint-lawrence/Fixate/tree/mypy-experiments

Not free of errors (~10 left), but I did fix a fair few.

daniel-montanari commented 2 years ago

Me just playing. Didn't think too hard about it, just wanted to see what kind of progress I could make https://github.com/clint-lawrence/Fixate/tree/mypy-experiments

Not free of errors (~10 left), but I did fix a fair few.

_src\fixate\drivers\lcr\agilentu1732c.py:95: error: Signature of "frequency" incompatible with supertype "LCR"

_src\fixate\drivers\funcgen\rigol_dg1022.py:399: error: Name "amplitudech1" already defined on line 391

_src\fixate\drivers\funcgen\rigoldg1022.py:449: error: "Callable[[FuncGen], Any]" has no attribute "setter"

_src\fixate\drivers\pps__init.py:22: error: Incompatible types in assignment (expression has type "BK178X", variable has type "SPD3303X") src\fixate\drivers\pps\init__.py:23: error: "SPD3303X" has no attribute "baudrate"

daniel-montanari commented 2 years ago
if struct.calcsize("P") == 8:  # 64 bit
    FT_HANDLE = ctypes.c_ulonglong
else:
    FT_HANDLE = ctypes.c_ulong

Mypy forces you to either rewrite this as a conditional, or explicitly annotate the type as a Union yourself :(

clint-lawrence commented 2 years ago
if struct.calcsize("P") == 8:  # 64 bit
    FT_HANDLE = ctypes.c_ulonglong
else:
    FT_HANDLE = ctypes.c_ulong

Mypy forces you to either rewrite this as a conditional, or explicitly annotate the type as a Union yourself :(

FH_HANDLE = ctypes.c_void_p should fix that and is a more appropriate type. It is not a Union!

daniel-montanari commented 1 year ago

I've updated the exclude list since files have changed in the last few months. I've also added all excluded files to the silent imports list. This means that importing an excluding module won't create errors due to mypy analysing the excluded module when it is imported by another module. I've also updated mypy to the latest version.

I haven't gone through and changed any of the config rules to non-default. We can merge this first and then update the config later.

jcollins1983 commented 1 year ago

Overall, I'm happy to merge. I think you should get buy in from everyone first though. (@John2202W , @jcollins1983 , @christopherwallis , @Jasper-Harvey0 )

The main thing I'm wary off is not having a clear goal stated. For example, that goal could be

  • Have all interfaces intended for use in scripts annotated
  • Have the entire codebase annotated

The first should probably be the priority. (e.g. we spend more time writing scripts than modifying fixate)

And I've already said this before, but not sure I've committed it in writing - we almost certainly shouldn't try annotating the code as is. There is all kinds of busted stuff that most likely needs to be refactored and/or redesigned before annotation will be useful.

I'm generally in favour of this, particularly if it means you don't have to go digging into functions that are supposed to be providing a layer of abstraction to work out what they actually need to be provided, so I agree with Clint that the priority should be to annotate all script facing interfaces.

John2202W commented 1 year ago

I'm in favour of gradual introduction of type hinting - haven't played much with mypy but appears a good route.

christopherwallis commented 1 year ago

I'm definitely supportive of this too. Should (eventually) make a lot of things easier, especially the changes to script interfaces. I haven't tested anything and I'm not completely up to date with fixate or mypy but everything looks good based on my read through of the pull request. The comment about caching with @daniel-montanari's last change might need updating though.