Skip to content

Stubgen omits namedtuple fields for subclasses. #9901

Closed
@domdfcoding

Description

@domdfcoding

Bug Report

To Reproduce

Consider the common pattern of subclassing the output of the namedtuple() function to add extra methods:

from collections import namedtuple

class EmployeeRecord(namedtuple('EmployeeRecord', 'name, age, title, department, paygrade')):
    
	def is_junior():
		return self.age < 18
  1. Save the above code to a file.
  2. Run stubgen on it.

Expected Behavior

I would have expected the stubs generated by stubgen to include the namedtuple's fields (name, age, etc.), typed as Any if no type can be inferred.
I expected the output to look something like:

from typing import NamedTuple

class EmployeeRecord(NamedTuple):
	name: Any
	age: Any
	title: Any
	department: Any
	paygrade: Any
	def is_junior(): ...

Actual Behavior

However, the output is instead:

class EmployeeRecord:
	def is_junior(): ...

EmployeeRecord doesn't subclass from NamedTuple and doesn't have the expected attributes.

Your Environment

  • Mypy version used: 0.790 and 0.800+dev.e9edcb957f341dcf7a14bb7b7acfd4186fb1248b
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: Python 3.8.5
  • Operating system and version: Ubuntu 20.04

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions