Re: Python static type checking

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



(moved over from PR discussion)

>>> @p-na
>>> Even if we had a type checker integration in make check, it
>>> still could be considered optional, as no one is enforced
>>> to write type hints, even though they are there.
>> 
>> @sebastian-philipp
>> Well, the purpose of make check is to check and verify stuff,
>> especially as make check doesn't print anything, if the test passes.
>> 
>> So, how could an optional make check integration look like?
> 
> @p-na
> The check itself wouldn't be optional, it would be up to the developer to write new type hints or not. That's "optional" in a wide sense, though.
> 
> make check would then run mypy to verify the types and exit properly if everything is fine, otherwise return a non-zero exit code, which could be used to determine the failure of the test.
> 
> This would not enforce anyone to write type hints but it would enforce that the types, which are already there, are valid.
> 
> This is not a proposal, I just wanted to add that this would also be possible, provided that the interpretation of "optional" leaves enough room for this implementation.

Patrick,

yep, this wouldn't enforce type hints, but it would have the big
downside of forcing everyone to fix false-positives, like

> pybind/mgr/prometheus/module.py: note: In member "str_expfmt" of class "Metric":
> pybind/mgr/prometheus/module.py:139: error: Incompatible types in assignment (expression has type "str", variable has type "Iterator[Tuple[Any, Any]]")





Am 04.03.19 um 10:16 schrieb Sebastian Wagner:
> all,
> 
> to find bugs in our Python code base a bit earlier, I looked into static
> type checking using mypy and I'd like to share my results.
> 
> Mypy ( http://mypy-lang.org/ ) is a static type checker for Python that
> aims to combine the benefits of dynamic (or "duck") typing and static
> typing.
> 
> As of now, running mypy on our code base already reveals some genuine
> bugs, especially with respect to Python 3 incompatibilities.
> 
> Unfortunately, we will need to look into every module and message and
> manually separate bugs and false-positives.
> 
> Here are two example errors:
> 
>> pybind/mgr/restful/decorators.py:21: error: 
>>   Argument 1 to "split" of "bytes" has incompatible type "str"; expected "Optional[bytes]" 
> 
> 
> This is an issue that will be fixed by
> https://github.com/ceph/ceph/pull/26712
> 
>> pybind/mgr/diskprediction_cloud/module.py:12: error:
>>   Module 'string' has no attribute 'maketrans'
> 
> 
> which is already tracked as https://tracker.ceph.com/issues/38521
> 
> By adding additional type annotations to our Python code, we could
> enhance the quality of static type checking and thus reveal additional
> bugs. This is of course not mandatory!
> 
> 
> Here is the PR that adds a script that calls mypy:
> https://github.com/ceph/ceph/pull/26715
> 
> It also contains a list of messages printed by mypy.
> 
> 
> 
> 
> 

-- 
SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton,
HRB 21284 (AG Nürnberg)



[Index of Archives]     [CEPH Users]     [Ceph Large]     [Information on CEPH]     [Linux BTRFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux