坐标系统与单位规范
- 长度单位:微米(μm)
- 旋转单位:度(°),范围[-180, 180]
- 角度表示:优先使用正角度(逆时针旋转)
- 参数命名:所有角度参数需添加
_deg后缀(如rotation_deg)
### 类型注解强化
使用Python类型注解扩展,创建语义化角度类型:
```python
from typing import NewType
Degrees = NewType('Degrees', int) # 度为单位的角度
Radians = NewType('Radians', float) # 弧度为单位的角度
# 使用类型注解
def safe_rotate(component, rotation: Degrees):
component.rotate(rotation)
# 调用时明确类型
safe_rotate(straight, rotation=Degrees(90))
自动化检测脚本
添加pre-commit钩子检测模糊的旋转参数使用:
# .pre-commit-config.yaml
repos:
- repo: local
hooks:
- id: check-rotation-units
name: Check rotation units
entry: python scripts/check_rotation_units.py
language: system
files: '.*\.py$'
检测脚本核心逻辑:
# scripts/check_rotation_units.py
import re
import sys
def check_rotation_units(file_path):
with open(file_path) as f:
content = f.read()
# 检测未标注单位的rotation参数
pattern = r'rotation\s*=\s*\d+\s*[,)]'
if re.search(pattern, content):
print(f"警告: {file_path} 中发现未标注单位的rotation参数")
return 1
return 0
sys.exit(check_rotation_units(sys.argv[1]))
总结与展望
gdsfactory的grid()函数旋转参数单位问题,反映了科学计算与工程设计在单位系统上的天然矛盾。通过本文提供的:
- 显式单位标注法
- 类型安全转换机制
- 自动化检测工具
可有效规避此类问题。未来版本中,建议gdsfactory官方采用更明确的参数设计:
# 理想的API设计
def grid(
# ...
rotation_deg: int = 0, # 明确标注单位
# 或提供单位选择参数
rotation_unit: Literal['deg', 'rad'] = 'deg',
):
pass
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考



