Skip to content

add ErrorAsType and related functions#1861

Draft
mattjohnsonpint wants to merge 3 commits intostretchr:masterfrom
mattjohnsonpint:mjp/errorastype
Draft

add ErrorAsType and related functions#1861
mattjohnsonpint wants to merge 3 commits intostretchr:masterfrom
mattjohnsonpint:mjp/errorastype

Conversation

@mattjohnsonpint
Copy link

Summary

Adds Testify functions that mirror the new errors.AsType[E] in Go 1.26.

Changes

For Go 1.26+ consumers, adds the following:

assert.ErrorAsType[E error](t TestingT, err error, msgAndArgs ...any) (E, bool)
assert.ErrorAsTypef[E error](t TestingT, err error, msg string, args ...any) (E, bool)
assert.NotErrorAsType[E error](t TestingT, err error, msgAndArgs ...any) bool
assert.NotErrorAsTypef[E error](t TestingT, err error, msg string, args ...any) bool

require.ErrorAsType[E error](t TestingT, err error, msgAndArgs ...any) E
require.ErrorAsTypef[E error](t TestingT, err error, msg string, args ...any) E
require.NotErrorAsType[E error](t TestingT, err error, msgAndArgs ...any)
require.NotErrorAsTypef[E error](t TestingT, err error, msg string, args ...any)

Note that E is returned where appropriate, to allow the caller to follow with additional requirements without further casting.

Motivation

Instead of:

var syntaxErr *json.SyntaxError
require.ErrorAs(t, err, &syntaxErr)

With this change, and if I'm using Go 1.26.0 or later, I can now write:

require.ErrorAsType[*json.SyntaxError](t, err)

Other functions are similar in usage to their existing *ErrorAs* counterparts.

Related issues

Closes #1860

@mattjohnsonpint mattjohnsonpint marked this pull request as draft March 13, 2026 00:53
@mattjohnsonpint mattjohnsonpint marked this pull request as ready for review March 13, 2026 00:58
Copy link
Collaborator

@ccoVeille ccoVeille left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds reasonable. I didn't check by pulling the branch, but the first look seems OK .

I was afraid you might break old Go versions by using a recent feature, but you used gu:build tag 👍

My only concern now is to check if the new files you add are auto generated or not.

Comment on lines +166 to +171
// Skip generic functions (type parameters present) — they cannot be
// reproduced by codegen and are maintained by hand.
if sig.TypeParams() != nil && sig.TypeParams().Len() > 0 {
continue
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure what this could mean and what would be the problems if you are not adding this to the PR

Copy link
Collaborator

@dolmen dolmen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To introduce generic functions in assert and require the code generator has to be improved.

Introducing generic functions without codegen will be too heavy to maintain.

- "1.23"
- "1.24"
- "1.25"
- "1.26"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This must be a separate PR.

}

// Skip generic functions (type parameters present) — they cannot be
// reproduced by codegen and are maintained by hand.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the way we must take.

To introduce generics in Testify, someone has to do the job of porting the code generator to support generics.

@dolmen
Copy link
Collaborator

dolmen commented Mar 13, 2026

In Testify assert, functions are also exposed as methods of the Assertions type.

Go doesn't yet supports methods with type parameters. While this is coming (see golang/go#77273), we aren't there yet. We need changes in the codegen that will be ready to handle methods with type parameters once they are there.

@dolmen dolmen added pkg-assert Change related to package testify/assert pkg-require Change related to package testify/require internal/codegen Change related to internal code generation generics labels Mar 13, 2026
@mattjohnsonpint
Copy link
Author

Thanks for the feedback. I'm away for the weekend but will revisit this soon.

@mattjohnsonpint mattjohnsonpint marked this pull request as draft March 13, 2026 20:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

generics internal/codegen Change related to internal code generation pkg-assert Change related to package testify/assert pkg-require Change related to package testify/require

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add require.ErrorAsType for Go 1.26+

3 participants