Skip to content

dtype.__repr__: prefer __name__ for user-defined types.#28250

Merged
charris merged 5 commits intonumpy:mainfrom
jakevdp:dtype-repr
Jan 31, 2025
Merged

dtype.__repr__: prefer __name__ for user-defined types.#28250
charris merged 5 commits intonumpy:mainfrom
jakevdp:dtype-repr

Conversation

@jakevdp
Copy link
Copy Markdown
Contributor

@jakevdp jakevdp commented Jan 29, 2025

The current approach can result in a misleading repr() for user-defined dtypes. For example, a bfloat16 scalar type that inherits from np.number and sets dtype.kind='f' will be displayed as dtype('float16') rather than dtype(bfloat16). We can circumvent this by handling user-defined dtypes first.

@charris
Copy link
Copy Markdown
Member

charris commented Jan 29, 2025

Could use a release note, as it is a behavior change.

@jakevdp
Copy link
Copy Markdown
Contributor Author

jakevdp commented Jan 29, 2025

Added a release note and a test. The test doesn't really cover the motivating issue here, as rational does not inherit from np.number, but it's better than nothing. It may be worth adding some sort of test-only custom float type to better cover this sort of issue, but I'll punt on that for now.

@ngoldbaum
Copy link
Copy Markdown
Member

There’s also the scaled float test dtype using the new dtype system. I want to add numpy-user-dtypes as a test-only dependency to make it easier to test stuff like this.

@ngoldbaum
Copy link
Copy Markdown
Member

The behavior change seems fine to me and reads like what the code intended beyond that no one expected someone to write a user dtype set up like bfloat16.

@charris
Copy link
Copy Markdown
Member

charris commented Jan 30, 2025

Close/reopen. The test failure looks unrelated, but I would like to know what is going on.

@charris charris closed this Jan 30, 2025
@charris charris reopened this Jan 30, 2025
@charris
Copy link
Copy Markdown
Member

charris commented Jan 30, 2025

@jakevdp Could you try rebasing on current master to see if that fixes the build failure on CircleCI?

The current approach can result in a misleading repr() for user-defined dtypes. For example, a bfloat16 scalar type that inherits from np.number and sets dtype.kind='f' will be displayed as dtype('float16'). We can circumvent this by handling user-defined dtypes first.
@jakevdp
Copy link
Copy Markdown
Contributor Author

jakevdp commented Jan 30, 2025

rebased

@charris
Copy link
Copy Markdown
Member

charris commented Jan 30, 2025

@jakevdp Thanks, that fixed the build. There was still an undefined reference, which I have fixed.

Comment thread doc/release/upcoming_changes/27357.improvement.rst Outdated
@charris charris merged commit 6bc9058 into numpy:main Jan 31, 2025
@charris
Copy link
Copy Markdown
Member

charris commented Jan 31, 2025

Thanks @jakevdp .

@jakevdp jakevdp deleted the dtype-repr branch January 31, 2025 01:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants