astropy / astropy/astropy

Custom vounit instances are not recognized when defined via `def_unit`

Open
#12,126 1 comment 0 reactions 0 assignees View on GitHub
Bug units
Dominant language
Python
Stars
5.3k
Forks
2.2k
Avg merge
1d 19h
Merged PRs (30d)
75

Description

`vounit` has the concept of being able to define an explicit custom unit by using single quotes. We support if one defines a unit via,
```
In [2]: redshift = u.Unit("'redshift'", format='vounit')

In [3]: redshift.to_string()
Out[3]: 'redshift'

In [4]: redshift.to_string('vounit')
Out[4]: "'redshift'"
```
Given this, the failure for the following (found in #11786, hence the example) seems unexpected:
```
In [2]: redshift = u.def_unit(['redshift'], format={'vounit': "'redshift'"})

In [3]: redshift.to_string()
Out[3]: 'redshift'

In [4]: redshift.to_string('vounit')
ValueError: Unit "'redshift'" is not part of the VOUnit standard
```

It would seem to make sense to allow the above to work, either by using `u.format.vounit.VOUnit._def_custom_unit` at definition time, or by letting `_get_unit_name` use a given units `get_format_name` (and check it works as explicit custom unit).

cc @nstarman, @dhomeier

Contributor guide

Open the contributing guide

Research direction

Start by reading `u.format.vounit.VOUnit._def_custom_unit` and `_get_unit_name`, then compare how explicit custom units and `def_unit` instances reach those paths. Done means the reported `def_unit(['redshift'], format={'vounit': "'redshift'"})` example can format as VOUnit without raising the standard-unit ValueError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.