Skip to content

gh-138978: Fully qualified name for abc classes exception. - #156683

Open
skv0zsneg wants to merge 12 commits into
python:mainfrom
skv0zsneg:fix-issue-138978
Open

gh-138978: Fully qualified name for abc classes exception.#156683
skv0zsneg wants to merge 12 commits into
python:mainfrom
skv0zsneg:fix-issue-138978

Conversation

@skv0zsneg

@skv0zsneg skv0zsneg commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Comment thread Lib/test/test_embed.py

def check_program_exitcode(self, *args, check_stderr=True, **kwargs):
out, err = self.run_embedded_interpreter(*args, **kwargs)
self.assertEqual(out.rstrip(), 'ok! Py_RunMain() returned 123')

@skv0zsneg skv0zsneg Aug 30, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

After adding changes to tests and typeobjec.c the strange error appear

AssertionError: 'Cmd click to launch VS Code Native REPL\nok! Py_RunMain() returned 123' != 'ok! Py_RunMain() returned 123'
- Cmd click to launch VS Code Native REPL
  ok! Py_RunMain() returned 123

So, I changed it for checking exactly "ok!..." string.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you check if it happens from CLI? My hunch is that it's VS Code artifact

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Wow, yes. From CLI test is not falling.

I do not clearly understand what this test (and others in Lib/test/test_embed.py) checking. Integrations with IDE's?

@maurycy maurycy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I suspect that the C part can be a bit smaller, using %N

case 'N':
{
PyObject *type_raw = va_arg(*vargs, PyObject *);
assert(type_raw != NULL);
if (!PyType_Check(type_raw)) {
PyErr_SetString(PyExc_TypeError, "%N argument must be a type");
return NULL;
}
PyTypeObject *type = (PyTypeObject*)type_raw;
PyObject *type_name;
if (flags & F_ALT) {
type_name = _PyType_GetFullyQualifiedName(type, ':');
}
else {
type_name = PyType_GetFullyQualifiedName(type);
}
if (!type_name) {
return NULL;
}
if (unicode_fromformat_write_str(writer, type_name,
width, precision, flags) == -1) {
Py_DECREF(type_name);
return NULL;
}
Py_DECREF(type_name);
break;
}
default:

Comment thread Objects/typeobject.c Outdated
return NULL;
}

PyObject *type_name = _PyType_GetFullyQualifiedName(type, '.');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you check if PyErr_Format already does this via %N?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yep! It works, and all test (locally) passes! Amazing, thank you!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I wonder do docs have information about this cpython special string formats?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@@ -0,0 +1 @@
Add fully qualified name for abc class initialization exception.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe it should describe the user-visible change more, eg:

Suggested change
Add fully qualified name for abc class initialization exception.
The :exc:`TypeError` raised when instantiating an abstract class with unimplemented abstract methods now includes the fully qualified name of the class.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I love your version, thanks! I rewrite it like this

Comment thread Lib/test/test_abc.py Outdated
with self.assertRaises(TypeError) as cm:
self.MyAbstractClass()

print(str(cm.exception))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Left-over from debugging?

Suggested change
print(str(cm.exception))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh, how I missed this... Will remove

Comment thread Lib/test/test_abc.py Outdated
abc.get_cache_token)
class TestAbstractClassErrorMessage(unittest.TestCase):

class MyAbstractClass(abc.ABC):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think that abc_ABCMeta should be used here. My understanding is that test_factory creates a class twice - for C and Py variants - and hard-coded abc.ABC prevents the magic from happening :-)

@@ -0,0 +1 @@
The :exc:`TypeError` raised when instantiating an abstract class with unimplemented abstract methods now includes the fully qualified name of the class.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit:

Suggested change
The :exc:`TypeError` raised when instantiating an abstract class with unimplemented abstract methods now includes the fully qualified name of the class.
The :exc:`TypeError` raised when instantiating an abstract class with
unimplemented abstract methods now includes the fully qualified name of the
class.

I'm sorry for this one, was typing from my phone. Typically we wrap around ~79 chars, as per https://devguide.python.org/documentation/markup/#use-of-whitespace

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No problem! Will know, thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants