GithubHelp home page GithubHelp logo

Comments (10)

jparise avatar jparise commented on June 3, 2024 3

We actually now have code that attempts to traverse as much of the class hierarchy as is possible (given the AST's limitations). We use it to find Exception subclasses.

def get_classdef(cls, name, parents):
for parent in parents:
for node in parent.body:
if isinstance(node, ast.ClassDef) and node.name == name:
return node
@classmethod
def superclass_names(cls, name, parents, _names=None):
names = _names or set()
classdef = cls.get_classdef(name, parents)
if not classdef:
return names
for base in classdef.bases:
if isinstance(base, ast.Name) and base.id not in names:
names.add(base.id)
names.update(cls.superclass_names(base.id, parents, names))
return names

A path to implementing the suggestion in this issue might be:

  • Generalize the code above
  • Introduce an ignore-class-names (or similar) configuration option which skips all (or a subset of?) checks for matching subclasses
  • Consider defaulting the list of ignored class names to ["TypedDict"]

from pep8-naming.

gaborbernat avatar gaborbernat commented on June 3, 2024 1

I wasn't thinking to cover 100% of the cases but feels like covering the most common case (direct inheritance) as a low hanging fruit with a big benefit. It's fine e.g. to say we don't support typed dict definitions across files.

from pep8-naming.

sigmavirus24 avatar sigmavirus24 commented on June 3, 2024

We don't special case anything like this except what we default --ignore-names to. That would likely be the best place to configure this.

from pep8-naming.

gaborbernat avatar gaborbernat commented on June 3, 2024

I don't consider it a viable option, clearly, in this case, the PEP shouldn't mandate, and I don't think having to allowlist every name one by one is the path ahead. Also, I don't want these names to show up outside of a typed dictionary. So let us reconsider please.

from pep8-naming.

sigmavirus24 avatar sigmavirus24 commented on June 3, 2024

We would need significant refactoring to say "Ah, this has inherited umpteen times from a TypedDict and yes it's that TypedDict" which to handle 1 attribute here isn't reasonable. If we had anything like that, the unittest methods that are part of the default --ignore-names wouldn't be there but would be part of an exclusion applied to any class that inherits from unittest.TestCase.

The part where this becomes especially difficult is where we don't execute code we statically analyze it, so if someone inherits from some 3rd party library class that inherits from TypedDict, we aren't going to be able to detect that. We don't follow imports today, we don't want to (it adds significant time, fragility, and complexity - see also openstack's "hacking" plugin), and we'd still end up with issues like this.

from pep8-naming.

danielpatrickdotdev avatar danielpatrickdotdev commented on June 3, 2024

Since TypedDict is a special case, and this is much simpler to implement than adding a new option, I've raised #189 as a proposal to address that specific case

from pep8-naming.

sigmavirus24 avatar sigmavirus24 commented on June 3, 2024

--ignore-class-names fixes the problem of wanting to understand inheritance across modules, @danielpatrickdotdev, because we can't handle that otherwise as you rediscovered in #189.

from pep8-naming.

danielpatrickdotdev avatar danielpatrickdotdev commented on June 3, 2024

--ignore-class-names fixes the problem of wanting to understand inheritance across modules, @danielpatrickdotdev, because we can't handle that otherwise as you rediscovered in #189.

It does, but I'm never keen on blanket ignores like that, so was hoping for something more precise. I can live with something that covers common cases though

from pep8-naming.

sigmavirus24 avatar sigmavirus24 commented on June 3, 2024

Having a list of class names is far more precise than blanket ignoring any TypedDict inherited class which may or may not have non-snake-case attributes.

from pep8-naming.

danielpatrickdotdev avatar danielpatrickdotdev commented on June 3, 2024

In some ways it is, but it's quite an unweildy thing to define. I certainly wouldn't want to ignore all checks for a list of classes. Plus in this case we're interested in superclass names. I ended up with --ignore-variable-names-for-subclasses. Even that mouthful doesn't quite capture it.

from pep8-naming.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.