Skip to content

Move UnexpectedNilError into gem namespace#16

Merged
amomchilov merged 2 commits intomainfrom
Alex/namespace-unexpected-nil-error
Mar 3, 2026
Merged

Move UnexpectedNilError into gem namespace#16
amomchilov merged 2 commits intomainfrom
Alex/namespace-unexpected-nil-error

Conversation

@amomchilov
Copy link
Contributor

@amomchilov amomchilov commented Feb 28, 2026

Follow up to #7. Let's namespace the constant to TypeToolkit::UnexpectedNilError

@amomchilov
Copy link
Contributor Author

amomchilov commented Feb 28, 2026

This stack of pull requests is managed by Graphite. Learn more about stacking.

Comment on lines +13 to +16
UNEXPECTED_NIL_ERROR_NAME = ::TypeToolkit::UnexpectedNilError.name.not_nil!
.split("::").last.not_nil!
.to_sym #: Symbol
private_constant :UNEXPECTED_NIL_ERROR_NAME
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Un-hard-coding this, to make sure it stays in sync with the actual constant we care about.

@amomchilov amomchilov marked this pull request as ready for review February 28, 2026 00:54
@amomchilov amomchilov requested a review from a team February 28, 2026 00:54
# `UnexpectedNilError` should never occur in well-formed code, so it should never be rescued.
# This is why it inherits from `Exception` instead of `StandardError`,
# so that bare rescues clauses (like `rescue => e`) don't rescue it.
class UnexpectedNilError < Exception # rubocop:disable Lint/InheritException

Choose a reason for hiding this comment

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

Since it's now TypeToolkit::UnexpectedNilError I think it'll be good to add tests to look for raise usages with that full name too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. The tests got really duplicative, so I parameterized them. See the two commits separately

Choose a reason for hiding this comment

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

Oh nice, tests are much cleaner now. LGTM, there are some type checking errors though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed!

@Morriar Morriar self-assigned this Mar 2, 2026
# typed: true
# frozen_string_literal: true

require "type_toolkit/ext/nil_assertions"
Copy link
Contributor

Choose a reason for hiding this comment

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

Quick question: does loading this cop now patch NilClass and Kernel as a side effect? Users who configure the cop without wanting the runtime extensions would get them implicitly.

Was this the intended design, or should the constant be defined another way to avoid that side effect?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes it does,

and yeah I could extract out the error class to its own file, but I'll cross that bridge if someone has a really good reason to want that. Was hoping to be opinionated/KISS for now

class DontExpectUnexpectedNil < Base
RESTRICT_ON_SEND = [:assert_raises, :raise].freeze

UNEXPECTED_NIL_ERROR_NAME = ::TypeToolkit::UnexpectedNilError.name.not_nil!
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's intentional duplication, to make sure this is working correctly.

Base automatically changed from Alex/not_nil-follow-up to main March 2, 2026 18:55
@amomchilov amomchilov force-pushed the Alex/namespace-unexpected-nil-error branch from 9f868ae to 46be90a Compare March 3, 2026 18:52
@amomchilov amomchilov requested a review from KaanOzkan March 3, 2026 19:15
# `UnexpectedNilError` should never occur in well-formed code, so it should never be rescued.
# This is why it inherits from `Exception` instead of `StandardError`,
# so that bare rescues clauses (like `rescue => e`) don't rescue it.
class UnexpectedNilError < Exception # rubocop:disable Lint/InheritException

Choose a reason for hiding this comment

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

Oh nice, tests are much cleaner now. LGTM, there are some type checking errors though.

@amomchilov amomchilov force-pushed the Alex/namespace-unexpected-nil-error branch from 6526ab8 to b38c687 Compare March 3, 2026 21:37
@amomchilov amomchilov merged commit 40ee25e into main Mar 3, 2026
1 check passed
@amomchilov amomchilov deleted the Alex/namespace-unexpected-nil-error branch March 3, 2026 21:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants