GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Remove unreachable else-block in complex_subtype_from_string in Objects/complexobject.c · Issue #143830 · python/cpython

Remove unreachable else-block in complex_subtype_from_string in Objects/complexobject.c #143830

New issue
Copy link
New issue
Copy link
Closed
Closed
Remove unreachable else-block in complex_subtype_from_string in Objects/complexobject.c#143830
Copy link

Description

@b9788213
b9788213
opened on Jan 14, 2026
Issue body actions

Summary: While performing code coverage analysis (using gcov) on the complex object implementation, I noticed that the else block in complex_subtype_from_string is logically unreachable under the current calling conventions.

Details: The function complex_subtype_from_string(PyTypeObject *type, PyObject *v) contains a check:


if (PyUnicode_Check(v)) {
    // ... logic for unicode ...
}
else {
    PyErr_Format(PyExc_TypeError, "complex() argument must be a string or a number, not %T", v);
    return NULL;
}

However, this function is only called from complex_new in Objects/complexobject.c. In complex_new, there is already a guard clause that ensures complex_subtype_from_string is only invoked if PyUnicode_Check returns true:

if (PyUnicode_Check(arg)) {
    return complex_subtype_from_string(type, arg);
}

Because of this double-check, the else block in complex_subtype_from_string is never executed, even when passing invalid types (like bytes) to a complex subclass. In such cases, the error is handled elsewhere or the function is never reached.

Proposed Change: Since complex_subtype_from_string is a static helper function, we can either:

Remove the else block to clean up the code.

Replace the if (PyUnicode_Check(v)) with an assert(PyUnicode_Check(v)); to maintain safety in debug builds while removing unreachable production code.

Impact:

Slightly cleaner C code.

Improved code coverage metrics.

Removal of redundant type checking.

Activity

  1. skirpichev commented on Jan 14, 2026

    @skirpichev
    skirpichev
    on Jan 14, 2026
    Member
    More actions

    See #109642

  2. b9788213
    closed this as completedon Jan 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.