Skip to content

Commit

Permalink
simplify and optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Jul 22, 2024
1 parent 252e9e7 commit b804b8e
Showing 1 changed file with 3 additions and 15 deletions.
18 changes: 3 additions & 15 deletions src/textual/css/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,21 +279,9 @@ def only_one(
the_one: ExpectType | QueryType = (
self.first(expect_type) if expect_type is not None else self.first()
)
try:
# Now see if we can access a subsequent item in the nodes. There
# should *not* be anything there, so we *should* get an
# IndexError. We *could* have just checked the length of the
# query, but the idea here is to do the check as cheaply as
# possible. "There can be only one!" -- Kurgan et al.
_ = self.nodes[1]
raise TooManyMatches(
"Call to only_one resulted in more than one matched node"
)
except IndexError:
# The IndexError was got, that's a good thing in this case. So
# we return what we found.
pass
return the_one
if len(self.nodes) == 1:
return the_one
raise TooManyMatches("Call to only_one resulted in more than one matched node")

if TYPE_CHECKING:

Expand Down

0 comments on commit b804b8e

Please sign in to comment.