I've got the following function, which given a string of the form 'a-02/b-03/foobarbaz_c-04'
, will extract the digits after a, b and c. The issue is that, for my use case, the input strings may not contain c, such that there will be no digits to extract.
Here's the code:
from typing import Tuple, Optional
def regex_a_b_c(name: str) -> Tuple[int, int, Optional[int]]:
a_b_info = re.search('a-(\d\d)/b-(\d\d)/', name)
a, b = [int(a_b_info.group(x)) for x in range(1, 3)]
c_info = re.search('c-(\d\d)', name)
if c_info:
c = int(c_info.group(1))
else:
c = None
return a, b, c
The issue I have is that, despite trying to make it clear that the last return argument is an Optional[int]
, I can't get my linter to stop complaining about the variable c.
I get a warning at the line c = None
that says:
Incompatible types in assignment (expression has type None, variable has type int)
How can I solve the issue?