Skip to content

Commit b6ba669

Browse files
authored
tooling: Update minimum go version to 1.22, update golangci-lint (tmc#722)
* go: Update to go 1.22, update golangci-lint config * lint: Address various lint issues * chains: fix lint complaint in TestApplyWithCanceledContext * lint: Address addtional lint issues * lint: Address addtional lint issues * tools: update golangci-lint to 1.57
1 parent 3932b31 commit b6ba669

32 files changed

Lines changed: 69 additions & 72 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
uses: golangci/golangci-lint-action@v3.7.0
2424
with:
2525
args: --timeout=4m
26-
version: v1.55.1
26+
version: v1.57.1
2727
build-examples:
2828
runs-on: ubuntu-latest
2929
steps:

.golangci.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ linters:
2828
- nolintlint # see https://github.com/golangci/golangci-lint/issues/3228.
2929
- depguard # disabling temporarily
3030
- ireturn # disabling temporarily
31+
- perfsprint
32+
- musttag
3133

3234
linters-settings:
3335
cyclop:
@@ -48,5 +50,5 @@ linters-settings:
4850
- "**/*_test.go"
4951
- "**/mock/**/*.go"
5052
run:
51-
skip-dirs:
53+
exclude-dirs:
5254
- 'exp'

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ lint-all:
2626
lint-deps:
2727
@command -v golangci-lint >/dev/null 2>&1 || { \
2828
echo >&2 "golangci-lint not found. Installing..."; \
29-
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.55.1; \
29+
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.57.1; \
3030
}
3131

3232
.PHONY: docs

chains/chains_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,16 @@ func TestApplyWithCanceledContext(t *testing.T) {
101101
wg.Add(1)
102102
c := NewLLMChain(&testLanguageModel{simulateWork: time.Second}, prompts.NewPromptTemplate("test", nil))
103103

104+
var applyErr error
104105
go func() {
105106
defer wg.Done()
106-
_, err := Apply(ctx, c, inputs, maxWorkers)
107-
require.Error(t, err)
107+
_, applyErr = Apply(ctx, c, inputs, maxWorkers)
108108
}()
109109

110110
cancelFunc()
111111
wg.Wait()
112+
113+
if applyErr == nil || applyErr.Error() != "context canceled" {
114+
t.Fatal("expected context canceled error, got:", applyErr)
115+
}
112116
}

chains/sequential_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ func TestSimpleSequentialErrors(t *testing.T) {
6666
}
6767

6868
for _, tc := range testCases {
69-
tc := tc
7069
t.Run(tc.name, func(t *testing.T) {
7170
t.Parallel()
7271
c, err := NewSimpleSequentialChain([]Chain{tc.chain})
@@ -179,7 +178,6 @@ func TestSequentialChainErrors(t *testing.T) {
179178
}
180179

181180
for _, tc := range testCases {
182-
tc := tc
183181
t.Run(tc.name, func(t *testing.T) {
184182
t.Parallel()
185183
c, err := NewSequentialChain(tc.chains, []string{"input1", "input2"}, []string{"output"}, tc.seqChainOpts...)

chains/stuff_documents_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ func TestStuffDocuments_joinDocs(t *testing.T) {
8686
chain := NewStuffDocuments(&LLMChain{})
8787

8888
for _, tc := range testcases {
89-
tc := tc
9089
t.Run(tc.name, func(t *testing.T) {
9190
t.Parallel()
9291
got := chain.joinDocuments(tc.docs)

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module github.com/tmc/langchaingo
22

3-
go 1.21
4-
5-
toolchain go1.21.4
3+
go 1.22
64

75
require (
86
github.com/google/uuid v1.6.0

jsonschema/json_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ func TestDefinition_MarshalJSON(t *testing.T) { //nolint:funlen
172172
}
173173

174174
for _, tt := range tests {
175-
tt := tt
176175
t.Run(tt.name, func(t *testing.T) {
177176
t.Parallel()
178177
wantBytes := []byte(tt.want)
@@ -184,7 +183,7 @@ func TestDefinition_MarshalJSON(t *testing.T) { //nolint:funlen
184183
}
185184

186185
got := structToMap(t, tt.def)
187-
gotPtr := structToMap(t, &tt.def)
186+
gotPtr := structToMap(t, &tt.def) //#nosec G601 -- false positive now that we're on go 1.22+
188187

189188
if !reflect.DeepEqual(got, want) {
190189
t.Errorf("MarshalJSON() got = %v, want %v", got, want)

llms/cache/cache.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,16 @@ func (c *Cacher) GenerateContent(ctx context.Context, messages []llms.MessageCon
8181
return response, nil
8282
}
8383

84-
// hashKeyForCache implements a hair-brained hashing scheme for the parameters to `GenerateContent`.
85-
// It simply marshals all parameters as JSON and hashes the result.
84+
// hashKeyForCache is a helper function that generates a unique key for a given
85+
// set of messages and call options.
8686
func hashKeyForCache(messages []llms.MessageContent, opts llms.CallOptions) (string, error) {
8787
hash := sha256.New()
8888
enc := json.NewEncoder(hash)
89-
9089
if err := enc.Encode(messages); err != nil {
9190
return "", err
9291
}
93-
9492
if err := enc.Encode(opts); err != nil {
9593
return "", err
9694
}
97-
9895
return hex.EncodeToString(hash.Sum(nil)), nil
9996
}

llms/cache/cache_test.go

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,33 @@ import (
1111
func TestCache_hashKeyForCache(t *testing.T) {
1212
t.Parallel()
1313

14-
rq := require.New(t)
15-
14+
cases := []struct {
15+
name string
16+
v1 []llms.MessageContent
17+
v1opt []llms.CallOption
18+
v2 []llms.MessageContent
19+
shouldMatch bool
20+
}{
21+
{
22+
name: "empty",
23+
v1: []llms.MessageContent{},
24+
v2: []llms.MessageContent{},
25+
shouldMatch: true,
26+
},
27+
{
28+
name: "empty vs non-empty",
29+
v1: []llms.MessageContent{},
30+
v2: []llms.MessageContent{{}},
31+
shouldMatch: false,
32+
},
33+
{
34+
name: "different options",
35+
v1: []llms.MessageContent{{}},
36+
v1opt: []llms.CallOption{llms.WithCandidateCount(1)},
37+
v2: []llms.MessageContent{{}},
38+
shouldMatch: false,
39+
},
40+
}
1641
mustHashKeyForCache := func(messages []llms.MessageContent, options ...llms.CallOption) string {
1742
var opts llms.CallOptions
1843
for _, opt := range options {
@@ -26,26 +51,16 @@ func TestCache_hashKeyForCache(t *testing.T) {
2651

2752
return key
2853
}
29-
30-
rq.Equal(
31-
mustHashKeyForCache([]llms.MessageContent{}),
32-
mustHashKeyForCache([]llms.MessageContent{}),
33-
)
34-
35-
rq.Equal(
36-
mustHashKeyForCache([]llms.MessageContent{}, llms.WithCandidateCount(1)),
37-
mustHashKeyForCache([]llms.MessageContent{}, llms.WithCandidateCount(1)),
38-
)
39-
40-
rq.NotEqual(
41-
mustHashKeyForCache([]llms.MessageContent{{}}),
42-
mustHashKeyForCache([]llms.MessageContent{}),
43-
)
44-
45-
rq.NotEqual(
46-
mustHashKeyForCache([]llms.MessageContent{}, llms.WithCandidateCount(1)),
47-
mustHashKeyForCache([]llms.MessageContent{}),
48-
)
54+
for _, tc := range cases {
55+
t.Run(tc.name, func(t *testing.T) {
56+
t.Parallel()
57+
v1hash := mustHashKeyForCache(tc.v1, tc.v1opt...)
58+
v2hash := mustHashKeyForCache(tc.v2)
59+
if (v1hash == v2hash) != tc.shouldMatch {
60+
t.Fatalf("expected %v, got %v", tc.shouldMatch, v1hash == v2hash)
61+
}
62+
})
63+
}
4964
}
5065

5166
func TestCache_Call(t *testing.T) {

0 commit comments

Comments
 (0)