Repository: python-babel/babel

Issue:
format_timedelta() for minutes=1 and hours=1 with narrow format and pt_BR locale returns an empty string
## Overview Description

format_timedelta() for minutes=1 and hours=1 with narrow format and pt_BR locale returns an empty string.

## Steps to Reproduce

```pycon
>>> import babel
>>> babel.__version__
'2.10.3'
>>> from datetime import timedelta
>>> from babel import dates
>>> dates.format_timedelta(timedelta(seconds=1), format='narrow', locale='pt_BR')
'1 s'
>>> dates.format_timedelta(timedelta(minutes=1), format='narrow', locale='pt_BR')
''
>>> dates.format_timedelta(timedelta(hours=1), format='narrow', locale='pt_BR')
''
>>> dates.format_timedelta(timedelta(days=1), format='narrow', locale='pt_BR')
'1 dia'
>>> dates.format_timedelta(timedelta(days=30), format='narrow', locale='pt_BR')
'1 mês'
>>> dates.format_timedelta(timedelta(days=365), format='narrow', locale='pt_BR')
'1 ano'
```

## Actual Results

See "Steps to Reproduce"

## Expected Results

Probably, `1 hora` and `1 min`.

## Reproducibility

Always.

## Additional Information

In common/main/pt.xml file:

* `duration-hour` has only `displayName` element
* `duration-minute` is missing

```xml
                <unitLength type="narrow">
                        ...
                        <unit type="duration-day">
                                <displayName>dia</displayName>
                                <unitPattern count="one">{0} dia</unitPattern>
                                <unitPattern count="other">{0} dias</unitPattern>
                                <perUnitPattern>{0}/dia</perUnitPattern>
                        </unit>
                        <unit type="duration-hour">
                                <displayName>hora</displayName>
                        </unit>
                        <unit type="duration-second">
                                <displayName>seg</displayName>
                                <unitPattern count="one">{0} s</unitPattern>
                                <unitPattern count="other">{0} s</unitPattern>
                                <perUnitPattern>{0}/seg</perUnitPattern>
                        </unit>
```


Discussion:
Seeing the same issue with locale `pl` 
I suspect the root cause is that `common/main/root.xml` file has the following rule for the `narrow` format, however `parse_unit_patterns` in `scripts/import_cldr.py` doesn't process the `alias` element.

```xml
		<unitLength type="narrow">
			<alias source="locale" path="../unitLength[@type='short']"/>
		</unitLength>
```



Patch:
diff --git a/babel/dates.py b/babel/dates.py
index 040820a6d..c2e4f0cc7 100644
--- a/babel/dates.py
+++ b/babel/dates.py
@@ -943,7 +943,14 @@ def _iter_patterns(a_unit):
             else:
                 yield unit_rel_patterns['past']
         a_unit = f"duration-{a_unit}"
-        yield locale._data['unit_patterns'].get(a_unit, {}).get(format)
+        unit_pats = locale._data['unit_patterns'].get(a_unit, {})
+        yield unit_pats.get(format)
+        # We do not support `<alias>` tags at all while ingesting CLDR data,
+        # so these aliases specified in `root.xml` are hard-coded here:
+        # <unitLength type="long"><alias source="locale" path="../unitLength[@type='short']"/></unitLength>
+        # <unitLength type="narrow"><alias source="locale" path="../unitLength[@type='short']"/></unitLength>
+        if format in ("long", "narrow"):
+            yield unit_pats.get("short")
 
     for unit, secs_per_unit in TIMEDELTA_UNITS:
         value = abs(seconds) / secs_per_unit
@@ -956,7 +963,8 @@ def _iter_patterns(a_unit):
             for patterns in _iter_patterns(unit):
                 if patterns is not None:
                     pattern = patterns.get(plural_form) or patterns.get('other')
-                    break
+                    if pattern:
+                        break
             # This really should not happen
             if pattern is None:
                 return ''


Test patch:
diff --git a/tests/test_dates.py b/tests/test_dates.py
index d3f2ad9e7..f25dd6146 100644
--- a/tests/test_dates.py
+++ b/tests/test_dates.py
@@ -742,3 +742,12 @@ def test_en_gb_first_weekday():
 
 def test_issue_798():
     assert dates.format_timedelta(timedelta(), format='narrow', locale='es_US') == '0s'
+
+
+def test_issue_892():
+    assert dates.format_timedelta(timedelta(seconds=1), format='narrow', locale='pt_BR') == '1 s'
+    assert dates.format_timedelta(timedelta(minutes=1), format='narrow', locale='pt_BR') == '1 min'
+    assert dates.format_timedelta(timedelta(hours=1), format='narrow', locale='pt_BR') == '1 h'
+    assert dates.format_timedelta(timedelta(days=1), format='narrow', locale='pt_BR') == '1 dia'
+    assert dates.format_timedelta(timedelta(days=30), format='narrow', locale='pt_BR') == '1 mês'
+    assert dates.format_timedelta(timedelta(days=365), format='narrow', locale='pt_BR') == '1 ano'