Yes, PyCharm (or other linters like mypy) may flag locals().update(Variables.__members__) as an issue because it’s dynamically modifying the local namespace, which static analysis tools cannot track properly.
Instead of locals().update(), a more Pythonic and PyCharm-safe way is using module-level unpacking:
TEMPERATURE_2M = "2m_temperature"
SNOW_DEPTH = "snow_depth"
SNOW_DENSITY = "snow_density"
TOTAL_PRECIPITATION = "total_precipitation"
SURFACE_SOLAR_RADIATION_DOWNWARDS = "surface_solar_radiation_downwards"
SURFACE_NET_SOLAR_RADIATION = "surface_net_solar_radiation"
SKIN_RESERVOIR_CONTENT = "skin_reservoir_content"
VOLUMETRIC_SOIL_WATER_LAYER_1 = "volumetric_soil_water_layer_1"
VOLUMETRIC_SOIL_WATER_LAYER_2 = "volumetric_soil_water_layer_2"
VOLUMETRIC_SOIL_WATER_LAYER_3 = "volumetric_soil_water_layer_3"
VOLUMETRIC_SOIL_WATER_LAYER_4 = "volumetric_soil_water_layer_4"
EVAPORATION_FROM_TOP_OF_CANOPY = "evaporation_from_the_top_of_canopy"
EVAPORATION_FROM_VEGETATION_TRANSPIRATION = "evaporation_from_vegetation_transpiration"
SNOW_EVAPORATION = "snow_evaporation"
WIND_10M_U_COMPONENT = "10m_u_component_of_wind"
WIND_10M_V_COMPONENT = "10m_v_component_of_wind"
TOTAL_CLOUD_COVER = "total_cloud_cover"
# Unpack variables directly into the module namespace
globals().update({k: v for k, v in Variables.__members__.items()})
# Now we can access the variables directly
print(TEMPERATURE_2M.value) # Output: "2m_temperature"
print(TOTAL_CLOUD_COVER.value) # Output: "total_cloud_cover"
✅ No PyCharm syntax errors – it avoids direct manipulation of locals()
✅ Maintains auto-completion support – PyCharm still recognizes the Enum members
✅ Safer & Explicit – We unpack only Enum.__members__ into globals(), avoiding unwanted side effects
✅ More Maintainable – Doesn’t break static analysis tools like mypy
If you are working on a larger project, a safer and structured approach is:
from Variables import Variables
# Assign to shorter names (optional)
TEMPERATURE_2M = Variables.TEMPERATURE_2M
SNOW_DEPTH = Variables.SNOW_DEPTH
TOTAL_CLOUD_COVER = Variables.TOTAL_CLOUD_COVER
print(TEMPERATURE_2M.value) # Output: "2m_temperature"
This avoids modifying globals() while keeping the code explicit and easy to follow.
- For scripts & quick prototyping →
globals().update({k: v for k, v in Variables.__members__.items()}) ✅
- For larger projects & maintainability → Explicit imports (
from Variables import Variables) ✅
Would you like any modifications or explanations? 🚀