Skip to content

Conversation

@hmc-cs-mdrissi
Copy link
Contributor

Summary

Add stubs for feature_column api. They fully cover documented api.

Most of functions return undocumented FeatureColumn classes. I've included stubs for FeatureColumn classes with some common easier methods, but those are incomplete and as they're all undocumented I did not put fallback getattr for each one.

already_sorted: bool = False

class RaggedFeature(NamedTuple):
class RowSplits(NamedTuple):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This mypy dislikes and I'm unsure how to type this. The real code uses collections.namedtuple and does have nested namedtuples. But mypy gives an error of,

Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]"

because of them. pyright is happy with them. I could move the nested namedtuples out and having them marked private like, _RowSplits(NamedTuple). That does disagree with runtime as RaggedFeature.RowSplits is the real way to access them, but unsure how to deal with it. Or maybe just type: ignore[misc] for them?

@github-actions

This comment has been minimized.

# typing.NamedTuple because they use multiple inheritance with other non namedtuple classes.
# _cls instead of cls is because collections.namedtuple uses _cls for __new__.
class NumericColumn(DenseColumn):
key: str
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As these are attributes from collections.namedtuple subclassing they are read only and could maybe be marked property? Sorta neutral to being more precise here unsure namedtuple fields are properties either. Especially as right now it's little more lenient and all these classes aren't documented so being stricter seems unnecessary.

Copy link
Member

Choose a reason for hiding this comment

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

This seems fine.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

class FixedLenFeature(NamedTuple):
shape: _ShapeLike
dtype: _DTypeLike
default_value: _TensorCompatible | None = None
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rchen152

  File "/Users/pa-loaner/Snapchat/Dev/.venvs/typeshed/lib/python3.9/site-packages/pytype/ast/visitor.py", line 55, in _call_visitor
    return visitor(node)
  File "/Users/pa-loaner/Snapchat/Dev/.venvs/typeshed/lib/python3.9/site-packages/pytype/pyi/parser.py", line 477, in visit_AnnAssign
    raise ParseError(msg)
pytype.pyi.types.ParseError:   File: "stubs/tensorflow/tensorflow/io/__init__.pyi", line 56
    default_value: _TensorCompatible | None = None
   ^
ParseError: Default value for default_value: typing.Union can only be '...', got None

I think this error happens with any default value in NamedTuple in a stub file. A more minimal example that also triggers an error like this is,

class Foo(NamedTuple):
  x: bool = False

I'll file a bug report on pytype as well. This only happens with .pyi files and namedtuple defaults work fine for .py files.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This also isn't an error that would be good to pytype_exclude_list as right now it causes a parse error so any file that imports this one also fails transitively and I think many tensorflow files would wind up needing to be excluded.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My workaround is just drop defaults for now and use ... and I'll add them back later after pytype allows namedtuple default values.

@hmc-cs-mdrissi
Copy link
Contributor Author

I think this is first typeshed pr I've had that found bug report for all 3 type checkers used in CI. A different bug for each one although all related sort of to NamedTuples.

@github-actions

This comment has been minimized.

@AlexWaygood
Copy link
Member

I think this is first typeshed pr I've had that found bug report for all 3 type checkers used in CI. A different bug for each one although all related sort of to NamedTuples.

Wow, you really hit the jackpot on this one!

from _typeshed import Incomplete
from collections.abc import Iterable

def rmtree(path: str) -> None: ...
Copy link
Member

Choose a reason for hiding this comment

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

Just double-checking, should these use _typeshed.StrOrPath?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks to be yes. Just tested and listdir works with Path[str]. Will test each manually and update them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All of them support Path except for glob. Also discovered they all support both str/bytes paths.

@JelleZijlstra
Copy link
Member

Also looks like a bunch of the # pyright: ignores can be removed.

Comment on lines 10 to 14
"stubs/**/@tests/test_cases",
// Bug in pyright that can't be ignored as it passes this one,
// but fails pyrightconfig.stricter.json. Can be removed after
// upgrading to 1.1.304 and removing ignores.
"stubs/tensorflow/tensorflow/io/__init__.pyi",
Copy link
Member

@AlexWaygood AlexWaygood Apr 26, 2023

Choose a reason for hiding this comment

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

Can this be removed now, as per #10052 (comment)?

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 it can be removed. I'll cleanup pyright ignores/this later today.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@JelleZijlstra JelleZijlstra merged commit 11f5858 into python:main Apr 27, 2023
srittau added a commit to srittau/typeshed that referenced this pull request Jul 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants