nvdlib icon indicating copy to clipboard operation
nvdlib copied to clipboard

Support for Python type hints

Open ammerzon opened this issue 1 year ago • 5 comments

Hi @vehemont. Thank you for this excellent library 😁

Have you considered adding type hints to function parameters, return values, and class attributes? Something along the way like this:

def searchCVE(
        cpeName: str=None,
        cveId: str=None,
        ...
) -> CVE:

ammerzon avatar Oct 12 '23 12:10 ammerzon

Hi,

I have thought about it but always decided against it. Since you have asked, I committed some type hints in 3fdd3f88ecc6ca05adcecc9fc65a2b0304c7630e. Is this what you had in mind?

Thanks

vehemont avatar Oct 12 '23 19:10 vehemont

Hi! Thank you for your quick changes. This is already very useful.

One of my biggest advantages would be if the return type is a defined object.

def searchCVE(...) -> CVE:

@dataclass
class CVE:
  descriptions: [Description]
  # ...

@dataclass
class Description
  lang: str
  value: str

ammerzon avatar Oct 15 '23 17:10 ammerzon

Also, I saw the usage of Tuple[str, datetime] for fields like modification or publication dates.

lastModStartDate: Tuple[str, datetime] = None,
lastModEndDate: Tuple[str, datetime] = None,

Shouldn't this be a | instead of Tuple[str, datetime] because the type should either be a string or a datetime object:

lastModStartDate: str | datetime = None,
lastModEndDate: str | datetime = None,

ammerzon avatar Nov 27 '23 14:11 ammerzon

The library is amazing, but having to suppress strict type checking on every call to the library can be a hassle. You mentioned you've “decided against it”. Does that mean PRs that enhance the type hints throughout the code are unwelcome? I'd love to contribute.

MatheusCylo avatar Jun 27 '24 17:06 MatheusCylo

The library is amazing, but having to suppress strict type checking on every call to the library can be a hassle. You mentioned you've “decided against it”. Does that mean PRs that enhance the type hints throughout the code are unwelcome? I'd love to contribute.

Please feel free to submit any PRs for type hints. I have not had the time to do research to learn proper type hints. My original statement was negligent and uninformed at that time, but since this issue was opened I have been trying to learn and implement them, just have not had time. In that original statement, I meant that when I was creating NVDLib I didn't think they would be useful. I can see how useful they are in documentation so I am in full support of adding type hints.

vehemont avatar Jun 27 '24 17:06 vehemont