arrow-py / arrow

🏹 Better dates & times for Python
https://arrow.readthedocs.io
Apache License 2.0
8.71k stars 673 forks source link

Refactor formatter to avoid nullable type bugs #918

Open systemcatch opened 3 years ago

systemcatch commented 3 years ago

mypy still complains about Optional.

arrow/formatter.py:125: error: Incompatible return value type (got "Optional[str]", expected "str")
arrow/formatter.py:142: error: Incompatible return value type (got "Optional[str]", expected "str")

First one is return dt.tzname() case. I know that Arrow always store non naive datetime now. but because DateTimeFormatter is public, someone could pass naive datetime object to DateTimeFormatter.format() and in that case DateTimeFormatter._format_token() will return None.

Second one is return self.locale.meridian(dt.hour, token).

But because those issues are known issue (I only annotate type for those cases), I think we can just leave FIXME comment and cast it as str for now. I want to minimize behavior changing in this PR.

_Originally posted by @isac322 in https://github.com/arrow-py/arrow/pull/883#discussion_r577341387_

https://github.com/isac322/arrow/blob/6aba5ed7626bfae2e1979354bd199aa526752cac/arrow/formatter.py#L128

return dt.tzname()

https://github.com/isac322/arrow/blob/6aba5ed7626bfae2e1979354bd199aa526752cac/arrow/formatter.py#L145

return self.locale.meridian(dt.hour, token)