Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix docstring for directly constructed types #1865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/runtime/ClassManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ internal static ClassManagerState SaveRuntimeData()
if ((Runtime.PyDict_DelItemString(dict.Borrow(), member) == -1) &&
(Exceptions.ExceptionMatches(Exceptions.KeyError)))
{
// Trying to remove a key that's not in the dictionary
// Trying to remove a key that's not in the dictionary
// raises an error. We don't care about it.
Runtime.PyErr_Clear();
}
Expand Down Expand Up @@ -215,7 +215,7 @@ internal static void InitClassBase(Type type, ClassBase impl, ReflectedClrType p
impl.indexer = info.indexer;
impl.richcompare.Clear();


// Finally, initialize the class __dict__ and return the object.
using var newDict = Runtime.PyObject_GenericGetDict(pyType.Reference);
BorrowedReference dict = newDict.Borrow();
Expand Down Expand Up @@ -271,6 +271,15 @@ internal static void InitClassBase(Type type, ClassBase impl, ReflectedClrType p
Runtime.PyDict_SetItem(dict, PyIdentifier.__doc__, doc.Borrow());
}
}

if (Runtime.PySequence_Contains(dict, PyIdentifier.__doc__) != 1)
{
// Ensure that at least some doc string is set
using var fallbackDoc = Runtime.PyString_FromString(
$"Python wrapper for .NET type {type}"
);
Runtime.PyDict_SetItem(dict, PyIdentifier.__doc__, fallbackDoc.Borrow());
}
}
doc.Dispose();

Expand Down Expand Up @@ -562,7 +571,7 @@ private static ClassInfo GetClassInfo(Type type, ClassBase impl)

return ci;
}

/// <summary>
/// This class owns references to PyObjects in the `members` member.
/// The caller has responsibility to DECREF them.
Expand Down
7 changes: 7 additions & 0 deletions tests/test_docstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,10 @@ def test_doc_without_ctor():
assert DocWithoutCtorTest.__doc__ == 'DocWithoutCtorTest Class'
assert DocWithoutCtorTest.TestMethod.__doc__ == 'DocWithoutCtorTest TestMethod'
assert DocWithoutCtorTest.StaticTestMethod.__doc__ == 'DocWithoutCtorTest StaticTestMethod'


def test_doc_primitve():
from System import Int64, String

assert Int64.__doc__ is not None
assert String.__doc__ is not None
5 changes: 2 additions & 3 deletions tests/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def test_enum_standard_attrs():
assert DayOfWeek.__name__ == 'DayOfWeek'
assert DayOfWeek.__module__ == 'System'
assert isinstance(DayOfWeek.__dict__, DictProxyType)
assert DayOfWeek.__doc__ is None


def test_enum_get_member():
Expand Down Expand Up @@ -139,7 +138,7 @@ def test_enum_undefined_value():
# This should fail because our test enum doesn't have it.
with pytest.raises(ValueError):
Test.FieldTest().EnumField = Test.ShortEnum(20)

# explicitly permit undefined values
Test.FieldTest().EnumField = Test.ShortEnum(20, True)

Expand All @@ -157,6 +156,6 @@ def test_enum_conversion():

with pytest.raises(TypeError):
Test.FieldTest().EnumField = "str"

with pytest.raises(TypeError):
Test.FieldTest().EnumField = 1