Skip to content

docs: fix examples that raise AttributeError when run as scripts - #5848

Merged
camdecoster merged 2 commits into
plotly:mainfrom
mmporong:doc-examples-attributeerror
Oct 28, 2026
Merged

camdecoster merged 2 commits into
plotly:mainfrom
mmporong:doc-examples-attributeerror

Conversation

@mmporong

@mmporong mmporong commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Link to issue

No issue. I found these while running the doc/python examples as plain Python scripts.

Description of change

Four doc pages have examples that raise AttributeError when run on their own:

  • 3d-mesh.md (two examples) calls np.DataSource(), which was removed in NumPy 2.0. It now uses np.lib.npyio.DataSource(), which exists in NumPy 2 and also in the NumPy 1.22.4 pinned in doc/requirements.txt.
  • graphing-multiple-chart-types.md (one example), sankey-diagram.md (two examples) and colorscales.md (four examples) do import urllib and then call urllib.request.urlopen(...). import urllib does not load the urllib.request submodule, so this fails in a fresh interpreter (module 'urllib' has no attribute 'request') and only works when something else has already imported it. They now import urllib.request.

Demo

Not applicable (no visual change).

Testing strategy

I ran every python block of 3d-mesh.md, graphing-multiple-chart-types.md and sankey-diagram.md in order in a fresh interpreter with plotly 7.1.0, NumPy 2.5.4 and pandas, with fig.show() stubbed out. Before the change, each page stopped with the AttributeError above. After it, all blocks on the three pages run. For colorscales.md, I ran each of the five blocks that use urllib in its own fresh process: before the change, the four blocks with import urllib failed with the AttributeError, and after it all five run. No tests are needed since only documentation examples change.

Guidelines

- 3d-mesh: np.DataSource was removed in NumPy 2.0; use
  np.lib.npyio.DataSource, which also exists in NumPy 1.x
- graphing-multiple-chart-types, sankey-diagram: `import urllib` does
  not load urllib.request, so import the submodule directly

@camdecoster camdecoster left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the fix! It's hard to keep the docs up to date. During review, I noticed that colorscales.md needs the same update. Would you be willing to take care of that here?

Four examples in colorscales.md use `import urllib` and then call
urllib.request.urlopen, which raises AttributeError when the example
runs on its own.
@mmporong

Copy link
Copy Markdown
Contributor Author

Thanks, done in ab658fb. The four examples in colorscales.md that did import urllib now import urllib.request. Running each of those blocks in a fresh process, they failed with the same AttributeError before the change and pass now.

I also searched the rest of doc/python again and found no other import urllib without the submodule; the other pages that use urllib already use from urllib.request import urlopen.

@camdecoster camdecoster added the no-changelog A changelog entry is not required for this PR label Oct 28, 2026
@camdecoster
camdecoster merged commit ad0d0bb into plotly:main Oct 28, 2026
31 of 32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-changelog A changelog entry is not required for this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants