Skip to content

objtype: Set __doc__ as None if not included #1550

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 2 commits into from
Oct 22, 2019
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
10 changes: 10 additions & 0 deletions tests/snippets/class.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ def b():
assert a == 2
A.b()

class A:
pass

assert A.__doc__ == None

class B:
"Docstring"

assert B.__doc__ == "Docstring"

# TODO: uncomment once free vars/cells are working
# The symboltable sees that b() is referring to a in the nested scope,
# so it marks it as non local. When it's executed, it walks up the scopes
Expand Down
7 changes: 6 additions & 1 deletion vm/src/obj/objtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,12 @@ fn type_new_slot(metatype: PyClassRef, args: PyFuncArgs, vm: &VirtualMachine) ->
bases
};

let attributes = dict.to_attributes();
let mut attributes = dict.to_attributes();

// insert __doc__ as None if it is not included in attributes
if !attributes.contains_key("__doc__") {
Copy link
Contributor

Choose a reason for hiding this comment

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

>>>>> def f():
.....  pass
..... 
>>>>> f.__doc__
'The most base type'

The current code does not check the code above.
The expected result is shown below.

>>> def f():
...  pass
... 
>>> f.__doc__
>>> 

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, this doesn't fixes the same problem in function.
I'm planning to handle that in another PR.

This only fixes class definition.

attributes.insert("__doc__".to_string(), vm.ctx.none());
}

let mut winner = metatype.clone();
for base in &bases {
Expand Down