Refactor formatter to avoid nullable type bugs
- Dominant language
- Python
- Stars
- 9.1k
- Forks
- 784
- PR merge metrics
- No merged PRs in 30d
Description
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
```python
return dt.tzname()
```
https://github.com/isac322/arrow/blob/6aba5ed7626bfae2e1979354bd199aa526752cac/arrow/formatter.py#L145
```python
return self.locale.meridian(dt.hour, token)
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start in arrow/formatter.py around lines 125 and 142, then run mypy to reproduce the two Optional[str] return errors. Keep the existing formatting behavior for naive datetimes and meridian formatting while addressing the annotations with the proposed FIXME and casts; done means mypy no longer reports these errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Refactor
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 58/100