Skip to content
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

How to differentiate class variables and instance variables? #30

Closed
tristanlatr opened this issue Aug 25, 2021 · 5 comments · Fixed by #38
Closed

How to differentiate class variables and instance variables? #30

tristanlatr opened this issue Aug 25, 2021 · 5 comments · Fixed by #38
Labels
component: docspec The main docspec module, or the docspec JSON specification. type: question Further information is requested

Comments

@tristanlatr
Copy link
Contributor

Is there a way currently to differentiate the two ?

I was thinking of naming the instance variables self.xxx, but maybe you have something else in mind?

Thanks

@NiklasRosenstein
Copy link
Owner

For properly typed Python code, it would be derivable from the type being wrapped in typing.ClassVar. Other than that, there is not currently a way to distinguish the two. When parsing the AST in pydoctor, how/where do you distinguish between the two? E.g. do you look for self.varname = ... statements in the classes' function bodies?

Some way of describing in docspec whether a Data is most likely an instance vs a class variable would probably make sense if it's not otherwise derivable (like from the typing.ClassVar annotation), but ideally I would still prefer if the client would be to make that distinction (such as with functions being methods vs classmethods vs staticmethods which is derivable from the function decorations).

@NiklasRosenstein NiklasRosenstein added component: docspec The main docspec module, or the docspec JSON specification. type: question Further information is requested labels Aug 25, 2021
@tristanlatr
Copy link
Contributor Author

tristanlatr commented Aug 25, 2021

Yes we do distinct the class variable with the instances variables by doing AST analysis. You can check it out here: https://github.com/twisted/pydoctor/blob/master/pydoctor/astbuilder.py#L517

do you look for self.varname = ... statements in the classes' function bodies?

That's basically what we're doing, we also look for typing.ClassVar annotation, but they are not really common. Also, a class variable do not need to be annotated as such to be properly recognized by pydoctor.

If we want to keep this outside of docspec module, I see only a naming convention with self as a valid solution.

@pawamoy
Copy link

pawamoy commented Aug 26, 2021

Hi, chiming in 🙂
I do the same in pytkdocs: the body of __init__ methods are parsed to get self.name = value or self.name: type = value statements. I don't parse any other method as it is considered bad practice to assign values to instance attributes that were not already 'declared' in the __init__ method.

@NiklasRosenstein
Copy link
Owner

👋 @pawamoy , thanks for your input as well.

I think this is a reasonable request. I currently have something like this in mind of the Data class:

@dataclasses.dataclass
class Data(ApiObject):
  """
  Represents a variable assignment (e.g. for global variables (often used as constants) or class members).
  """

  class Semantics(enum.Enum):
    #: The #Data object represents an instance variable.
    InstanceVariable = 0

    #: The #Data object represents a class variable.
    ClassVariable = 1

    #: The #Data object represents a constant.
    Constant = 2

  #: The datatype associated with the assignment as code.
  datatype: t.Optional[str] = None

  #: The value of the variable as code.
  value: t.Optional[str] = None

  #: A list of hints about the semantics of the variable that this #Data object describes.
  semantics: t.List[Semantics] = dataclasses.field(default_factory=list)

What do you think @tristanlatr @pawamoy ?

@tristanlatr
Copy link
Contributor Author

Looks good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: docspec The main docspec module, or the docspec JSON specification. type: question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants