Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

expand_dims(None) raises TypeError although None is its documented default

未关闭
#261 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@Doribelove 已经在做这个了。

开始于 2026年10月21日。

  • #270 来自 @Doribelove —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
52/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
python
领域
robotics

调研方向

Start with the expand_dims function in the spatialmath.base module, reading its signature, docstring and the code path that raises TypeError: invalid input type. Reproduce with expand_dims() and expand_dims(None). The issue leaves open whether to return None or remove the default, so confirm the maintainers' choice before editing. Done when the chosen behaviour holds and the existing suite still passes.

由索引模型根据 Issue 内容生成。

描述

spatialmath.base.expand_dims declares dim with a default of None and documents it (":param dim: dimensions, defaults to None"), but calling it with that value raises:

>>> from spatialmath.base import expand_dims
>>> expand_dims()
TypeError: invalid input type
>>> expand_dims(None)
TypeError: invalid input type

Found while consolidating how RTB resolves a plot or world extent from several candidate sources, where "not given" is a normal state (RTB cannot rely on the default, so it special-cases None before every call).

Current callers already guard it

Every caller in SMTB tests for None before calling, which is the check the function could do itself:

# graphics.py, plotvol2
if dim is None:
    ax.autoscale(True)
else:
    dims = expand_dims(dim, 2)

The same is true of plotvol3, Animate and the dims argument handling in transforms2d.py.

Suggestion

Return None for None:

def expand_dims(dim: ArrayLike | None = None, nd: int = 2) -> NDArray | None:
    if dim is None:
        return None
    ...
  • None is passed through as "unspecified", and the caller decides what that means (autoscale, no limits, use the next candidate).
  • A default box such as [-1, 1, -1, 1] would be worse: it invents a value the caller did not give, and would make "not given" indistinguishable from "given as 1".
  • A wrong-length vector still raises ValueError, so malformed input still fails loudly.
  • The return type becomes NDArray | None, so strictly typed callers need a None check. The existing call sites already have one.

Alternatively, if raising is intended, the signature and docstring should drop the default so that the argument is required.

Tested

With this change SMTB's own suite is unchanged (342 passed before and after). All four SMTB call sites already guard None. One downstream use, in bdsim's GraphicsBlock handling of scale=, would see an invalid scale=None fail later rather than at construction.

Related: #255 (the getvector/getmatrix converters, same family of "accept the natural input" questions).

主要语言
Python
星标
648
派生
108
平均合并
9 天 19 小时
30 天内合并 PR
9

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

rai-opensource/spatialmath-python 的其他 Issue

查看 rai-opensource/spatialmath-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。