MCPcopy Create free account
hub / github.com/python/mypy / enclosing_class

Method enclosing_class

mypy/checker_shared.py:321–330  ·  view source on GitHub ↗

Is there a class *directly* enclosing this function?

(self, func: FuncItem | None = None)

Source from the content-addressed store, hash-verified

319 return None
320
321 def enclosing_class(self, func: FuncItem | None = None) -> TypeInfo | None:
322 """Is there a class *directly* enclosing this function?"""
323 func = func or self.current_function()
324 assert func, "This method must be called from inside a function"
325 index = self.stack.index(func)
326 assert index, "CheckerScope stack must always start with a module"
327 enclosing = self.stack[index - 1]
328 if isinstance(enclosing, TypeInfo):
329 return enclosing
330 return None
331
332 def active_self_type(self) -> Instance | TupleType | None:
333 """An instance or tuple type representing the current class.

Calls 3

current_functionMethod · 0.95
isinstanceFunction · 0.85
indexMethod · 0.80

Tested by

no test coverage detected