mirror of
https://github.com/home-assistant/developers.home-assistant.git
synced 2025-07-19 15:26:29 +00:00
Add blog post about changes to UnitSystem
class and their implications (#2618)
* Add blog post about changes to UnitSystem class and their implications * Apply suggestions from code review Co-authored-by: Abílio Costa <abmantis@users.noreply.github.com> * Update blog/2025-03-24-frozen-unit-system.md Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Fix typo * Update filename --------- Co-authored-by: Abílio Costa <abmantis@users.noreply.github.com> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
5327735cea
commit
289f66817a
22
blog/2025-03-25-frozen-unit-system.md
Normal file
22
blog/2025-03-25-frozen-unit-system.md
Normal file
@ -0,0 +1,22 @@
|
||||
---
|
||||
author: Shay Levy
|
||||
authorURL: https://github.com/thecode
|
||||
title: "UnitSystem dataclass is now frozen"
|
||||
---
|
||||
|
||||
`UnitSystem` class is changed to a frozen data class, and all instances derived from it are now frozen.
|
||||
|
||||
The following unit systems that derived from the `UnitSystem` class are now frozen:
|
||||
|
||||
- `METRIC_SYSTEM`
|
||||
- `US_CUSTOMARY_SYSTEM`
|
||||
|
||||
The reason for this change is that Unit systems are constants that should not be modified. An integration that modifies these constants can break unit conversions and create undesired output for other components of Home Assistant.
|
||||
|
||||
With a frozen data class an attempt to modify the UnitSystem constant will fail:
|
||||
|
||||
```python
|
||||
dataclasses.FrozenInstanceError: cannot assign to field 'temperature_unit'
|
||||
```
|
||||
|
||||
This change was introduced in the [home assistant core PR #140954](https://github.com/home-assistant/core/pull/140954).
|
Loading…
x
Reference in New Issue
Block a user