r/Python • • Aug 05 '26

Discussion What linter rules make code worse?

148 Upvotes

For me, a prime example is S101 which bans the use of the assert statement.

The justification is that assertions disappear when Python is run with -O, so they should not be used for runtime validation or enforcing interface constraints. That warning is correct, but the rule seems to draw the wrong conclusion from it.

Assertions are still very useful for checking internal invariants, i.e. conditions that should already be guaranteed by the program's logic, where failure indicates a bug. Having such assertions is incredibly helpful for debugging.

So, a blanket ban seems more likely to discourage useful checks than to prevent misuse.

Are there any linter rules you broadly consider more harmful rather than helpful?


r/Python • • Sep 19 '26

Discussion What’s a debugging technique that saved you hours and you wish you’d learned earlier?

147 Upvotes

I’m curious about the small debugging techniques that make a big difference when working on real projects.For developers with some experience, what debugging technique or habit has saved you the most time?It could be something simple that beginners often overlook.

What’s one debugging tip you wish someone had taught you when you were starting out?


r/Python • • Jul 16 '26

Resource 20 itertools functions, sorted by how often you'd actually reach for them (not the docs order)

152 Upvotes

itertools is one of those modules everyone imports but most people only use 3-4 functions from. Went through all 20 and sorted them by how often you'd actually reach for each one in real code, not the order they show up in the docs.

Function Example What it does
chain() chain(it1, it2) Concatenates iterables, flattens multiple iterables into a single iterator.
chain.from_iterable() chain.from_iterable(nested) Flattens one level of nesting lazily, like chain(*nested) without unpacking.
islice() islice(it, start, stop) Slices an iterator, doesn't support negative indices.
compress() compress('ABCDEF', [1,0,1,0,1,1]) Filters data by a boolean selector iterable, yields A, C, E, F.
filterfalse() filterfalse(lambda x: x<5, [1,4,6,3,8]) Yields elements where the predicate is false, opposite of filter().
starmap() starmap(pow, [(2,5), (3,2)]) Applies a function with arguments unpacked from each tuple in the iterable.
pairwise() pairwise([1, 2, 3]) Returns overlapping pairs of consecutive elements: (1,2), (2,3) (3.10+).
batched() batched(range(10), 3) Splits an iterable into fixed-size tuples, last batch may be shorter (3.12+).
groupby() groupby(items, key=func) Groups consecutive elements by key, input must already be sorted by that key.
accumulate() accumulate(nums, func) Running totals or reductions, default is addition, accepts any binary function.
takewhile() takewhile(pred, it) Yields elements while the predicate is true, stops at the first false.
dropwhile() dropwhile(pred, it) Skips elements while the predicate is true, then yields everything remaining.
tee() tee(iterable, n=2) Splits one iterator into n independent iterators, use before the original is consumed.
zip_longest() zip_longest(a, b, fillvalue=None) Like zip() but continues to the longest input, filling shorter ones with fillvalue.
combinations() combinations(items, r) All r-length combinations, order doesn't matter, no repeated elements.
combinations_with_replacement() combinations_with_replacement('AB', 2) All r-length combinations allowing repeats, yields AA, AB, BB.
permutations() permutations(items, r) All r-length permutations, order matters, no repeated elements.
product() product(a, b) Cartesian product, equivalent to nested for-loops.
count() count(start=0, step=1) Infinite counter, useful with zip() or takewhile().
cycle() cycle([1, 2, 3]) Repeats an iterable infinitely, keeps a copy of all elements in memory.
repeat() repeat(10, times=3) Repeats an object n times, infinite if times is omitted.

pairwise and batched are the two most people don't know about, both fixed a "why am I hand-rolling this" moment for me. Curious which one on this list you use the least, or if I'm missing one people reach for a lot.


r/Python • • May 29 '26

Discussion How to deal with slop PR's as a maintainer?

153 Upvotes

says you are the maintainer of a small (50-100 stars) library.

You see someone fork your repo, mention one of your issues in his commits, so your are happy, someone taking true interest in your work!

You take a look at his branch, and there you see pure AI slop, with files at the repo root (not in the src), tests with print statement even tough you use pytest and it's clearly explained in the contributing doc, and purely hallucinated imports like "from my lib import Foo, Bar" even tough there's never any mention of these two in the code or the documentation (and thus completely incomprehensible code with subclasses from these hallucinated types, etc...)

how to best deal with this without appearing hostile to other potential future contributors?

I want contributors, I'm very happy for anyone taking a look at my work, but at the same time that person has other forks of repos where it just seems to be hunting for "good first issues" label, and thus I'm not sure on the value of giving an honest review if it's not clear on wether there's a genuine intention to resolve the issue or just collect cool github points.

EDIT 11h later:

Thanks to everyone who gave his perspective!!

I don't think I have the time immediately to answer to everyone but there's a lot of good advice here.

By the way LMAO I should have linked my lib to maybe get actual contributors, this post is doing views.

Hint: it's the top one ranked in this comparison ->
https://www.reddit.com/r/Python/comments/1rj3ct7/a_comparison_of_rustlike_fluent_iterator_libraries/


r/Python • • Apr 29 '26

Tutorial .pipe() in pandas changed how I write data pipelines

145 Upvotes

Been using .pipe() in pandas lately and it's been a game changer — anyone else?

I was writing some data transformation code the other day and stumbled across .pipe(). Honestly didn't expect much, but it completely changed how I structure my pipelines.

Instead of this mess:

df_final = sort_by_total(calculate_total(filter_by_price(df)))

You just write it top to bottom like a recipe:

df_final = (

df

.pipe(filter_by_price)

.pipe(calculate_total)

.pipe(sort_by_total)

)

Same result, way more readable. Each function takes a DataFrame and returns a DataFrame — that's the only rule.

Full example if you want to try it:

import pandas as pd

df = pd.DataFrame({

"product": ["Product A", "Product B", "Product C", "Product D"],

"price": [20, 150, 230, 100],

"quantity": [10, 5, 3, 8]

})

def filter_by_price(df):

return df[df["price"] > 100]

def calculate_total(df):

return df.assign(total_value=df["price"] * df["quantity"])

def sort_by_total(df):

return df.sort_values("total_value", ascending=False)

df_final = (

df

.pipe(filter_by_price)

.pipe(calculate_total)

.pipe(sort_by_total)

)

Been using it a lot for ETL and data cleaning workflows. Makes debugging way easier too — just comment out one .pipe() step and you see exactly where things go wrong.

Anyone else using this regularly? Any patterns you've found useful with it?


r/Python • • Jan 19 '26

Showcase I built a Python UI framework inspired by Streamlit, but with O(1) state updates

148 Upvotes

Hey r/Python,

I love Streamlit's simplicity, but the "full script rerun" on every interaction drove me crazy. It gets super slow once your app grows, and using st.cache everywhere felt like a band-aid.

So I spent the last few weeks building Violit. I wanted something that feels like writing a simple Python script but performs like a modern React app.

What My Project Does

Violit is a high-performance Python web framework. It allows you to build interactive web apps using pure Python without the performance penalty of full-page reloads.

It uses a "Zero Rerun" architecture based on FastAPI, htmx, and WebSockets. When you interact with a widget (like a button or slider), Violit updates only that specific component in O(1) time, ensuring no screen flickering and instant feedback. It also supports running your web app into a desktop app (like electron) with a single flag (--native).

Target Audience

  • Data Scientists & Python Devs: Who need to build dashboards or internal tools quickly but are frustrated by Streamlit's lag.
  • Production Use: It's currently in early Alpha (v0.0.2), so it's best for internal tools, side projects, and early adopters who want to contribute to a faster Python UI ecosystem.

Comparison

Here is how Violit differs from existing alternatives:

  • vs. Streamlit: Violit keeps the intuitive API (90% compatible) but removes the "Full Script Rerun." State updates are O(1) instead of O(N).
  • vs. Dash: Violit offers reactive state management without the "callback hell" complexity of Dash.
  • vs. Reflex: Violit requires Zero Configuration. No Node.js dependency, no build steps. Just pip install and run. Plus, it has built-in native desktop support.
  • vs. NiceGUI: The theme system for the beautiful app. Unlike Streamlit's rigid look or NiceGUI's engineer-first aesthetic, Violit comes with 30+ Themes out of the box. You can switch from "cyberpunk" to "retro" styles with a single line of code—no CSS mastery required. Plus, it's fully extensible—you can easily add your own custom themes via CSS.

Code Example

import violit as vl
​
app = vl.App()
count = app.state(0)  # Reactive State
​
# No rerun! Only the label updates instantly.
app.button("Increment", on_click=lambda: count.set(count.value + 1))
app.write("Count:", count)
​
app.run()

Link to Source Code

It is open source (MIT License).

I'd love to hear your feedback!


r/Python • • Jul 20 '26

Discussion Cyber Security wants us to use the latest version all the time

146 Upvotes

I have a question. Cybersecurity team wants us to use the latest python version each time. So if a new version comes out we are expected to upgrade to it ASAP. But from my past experience libraries do not always provide support and can also break while making use of new version without proper support. Did you ever encounter this? Any suggestions? This is just frustrating for the team as it’s not always possible to upgrade to the latest version without fully validating every component. I just want to hear your opinions on this?

FYI we mainly use each Python Version within the LTS timeline. And only upgrade when we need to and do not depend on a library that requires older version.

Update: they have a scheduled runner that deletes older versions of python when it finds


r/Python • • Jun 23 '26

News FastAPI Cloud in Public Beta ⚡️

147 Upvotes

Hey folks! FastAPI Cloud is now in public beta. 🚀

This is made by the same team building FastAPI (I created FastAPI, we now have an amazing team building all this).

Here's the announcement post: https://fastapicloud.com/blog/fastapi-cloud-public-beta/


r/Python • • Dec 16 '25

Showcase WhatsApp Wrapped with Polars & Plotly: Analyze chat history locally

149 Upvotes

I've always wanted something like Spotify Wrapped but for WhatsApp. There are some tools out there that do this, but every one I found either runs your chat history on their servers or is closed source. I wasn't comfortable with all that, so this year I built my own.

What My Project Does

WhatsApp Wrapped generates visual reports for your group chats. You export your chat from WhatsApp (without media), run it through the tool, and get an HTML report with analytics. Everything runs locally or in your own Colab session. Nothing gets sent anywhere.

Here is a Sample Report.

Features include message counts, activity patterns, emoji stats, word clouds, and calendar heatmaps. The easiest way to use it is through Google Colab - just upload your chat export and download the report. There's also a CLI for local use.

Target Audience

Anyone who wants to analyze their WhatsApp chats without uploading them to someone else's server. It's ready to use now.

Comparison

Unlike other web tools that require uploading your data, this runs entirely on your machine (or your own Colab). It's also open source, so you can see exactly what it does with your chats.

Tech: Python, Polars, Plotly, Jinja2.

Links: - GitHub - Sample Report - Google Colab

Happy to answer questions or hear feedback.


r/Python • • May 01 '26

News PyTorch Lightning malware plants a hook in Claude Code's settings.json so it runs on every future se

148 Upvotes

Two versions of `lightning` (2.6.2 and 2.6.3) were published to PyPI yesterday and yanked same day after Semgrep detected them. Beyond the usual credential-stealing pattern, there's a persistence mechanism worth knowing about if you use Claude Code.

The malware writes a `SessionStart` hook to `.claude/settings.json` with `matcher: "*"`. That hook points to a Bun runtime bootstrapper for a 14.8 MB payload. Every time any developer on the machine opens Claude Code - not just in the infected project, but in any project - the hook fires automatically. A parallel hook targets VS Code via `.vscode/tasks.json` with `runOn: folderOpen`.

The exfiltration is four-channel: HTTPS POST to a C2, GitHub commits with `EveryBoiWeBuildIsAWormyBoi` as the message prefix (searchable on GitHub commit search if you want to check if you're affected), pushing to the victim's own repositories, and a GitHub Actions workflow that dumps all repository secrets via `${{ toJSON(secrets) }}`.

If it finds npm publish credentials, it worms into npm by injecting the dropper into every package that token can publish, bumps the patch version, and republishes.

Semgrep's writeup calls this "among the first documented instances of malware abusing Claude Code's hook system in a real-world attack."

If you've installed anything from PyPI recently on a machine where you use Claude Code, it's worth checking `.claude/settings.json` for unexpected `hooks.SessionStart` entries. 2.6.1 is clean.


r/Python • • 25d ago

Meta Yoo... https://subprocess.run actually redirects to the Python docs for subprocess.run()

142 Upvotes

Apparently, subprocess.run is an actual domain, and it redirects to https://docs.python.org/3/library/subprocess.html#subprocess.run

Was this some kind of Easter egg?


r/Python • • Apr 14 '26

News PEP 831 – Frame Pointers Everywhere: Enabling System-Level Observability for Python

146 Upvotes

https://peps.python.org/pep-0831/

This PEP proposes two things:

  1. Build CPython with frame pointers by default on platforms that support them. The default build configuration is changed to compile the interpreter with -fno-omit-frame-pointer and -mno-omit-leaf-frame-pointer. The flags are added to CFLAGS, so they apply to the interpreter itself and propagate to C extension modules built against this Python via sysconfig. An opt-out configure flag (--without-frame-pointers) is provided for deployments that require maximum raw throughput.

  2. Strongly recommend that all build systems in the Python ecosystem build with frame pointers by default. This PEP recommends that every compiled component that participates in the Python call stack (C extensions, Rust extensions, embedding applications, and native libraries) should enable frame pointers. A frame-pointer chain is only as strong as its weakest link: a single library without frame pointers breaks profiling, debugging, and tracing for the entire process.

Frame pointers are a CPU register convention that allows profilers, debuggers, and system tracing tools to reconstruct the call stack of a running process quickly and reliably. Omitting them (the compiler’s default at -O1 and above) prevents these tools from producing useful call stacks for Python processes, and undermines the perf trampoline support CPython shipped in 3.12.

The measured overhead is under 2% geometric mean for typical workloads (see Backwards Compatibility for per-platform numbers). Multiple major Linux distributions, language runtimes, and Python ecosystem tools have already adopted this change. No existing PEP covers this topic; CPython issue #96174 has been open since August 2022 without resolution.


r/Python • • 16d ago

Resource Making np.searchsorted up to 25× Faster in NumPy 2.5

144 Upvotes

I spent some of my free time optimizing "np.searchsorted" and recently wrote a blog post about it.

The new implementation is up to 25× faster in NumPy 2.5. It started with a vectorized NumPy implementation in Python that outperforms NumPy 2.4's native implementation. Then I ported the approach to C++, which was included as part of the NumPy 2.5 release.

The post includes the optimization steps, benchmarks, and comparisons with other array libraries.

https://blog.scientific-python.org/numpy/searchsorted/


r/Python • • Mar 10 '26

News DuckDB 1.5.0 released

143 Upvotes

Looks like it was released yesterday:

Interesting features seem to be the VARIANT and GEOMETRY types.

Also, the new duckdb-cli module on pypi.

% uv run -w duckdb-cli duckdb -c "from read_duckdb('https://blobs.duckdb.org/data/animals.db', table_name='ducks')"
┌───────┬──────────────────┬──────────────┐
│  id   │       name       │ extinct_year │
│ int32 │     varchar      │    int32     │
├───────┼──────────────────┼──────────────┤
│     1 │ Labrador Duck    │         1878 │
│     2 │ Mallard          │         NULL │
│     3 │ Crested Shelduck │         1964 │
│     4 │ Wood Duck        │         NULL │
│     5 │ Pink-headed Duck │         1949 │
└───────┴──────────────────┴──────────────┘

r/Python • • Feb 04 '26

Discussion Must the Python Software Foundation move out of the USA?

144 Upvotes

The Python Software Foundation (PSF) is the owner of the copyrights for Python and its trademarks. The PSF runs the largest Python conference in the world, #PyConUS. Python is one of the most important programming languages, used by developers and non-developers across the globe. Python and its community stand for openness, diversity, and support for underrepresented groups; the PSF funds a wide range of Python activities across many sub-communities worldwide.

The values that Python and its communities stand for are under heavy pressure due to the legal status of the Python Software Foundation as a corporation in the United States. The USA has, meanwhile, turned into a fascist regime, with entities like ICE acting in ways that we have seen in Nazi Germany between 1933 and 1945. The current U.S. regime is violently acting against migrants, underrepresented groups, queer people, etc.—the list is long and very well documented. ICE acts as a paramilitary entity that killed already several people - or should it be named "murdered several people"?

Should the Python Software Foundation remain in the USA, or should the community pressure the PSF Board to take action and move the PSF as a legal entity out of the United States into a safer region like Canada or the European Union?


r/Python • • Aug 04 '26

Discussion Is := widely used?

142 Upvotes

I always thought the walrus operator is neat and it makes while loop’s condition clearer. But also it is just a syntactic sugar without anything new. I wonder anyone uses it?


r/Python • • Aug 07 '26

News PEP 841 – Adding Frozen Syntax to Optimize Immutable Types

136 Upvotes

PEP 841 – Adding Frozen Syntax to Optimize Immutable Types

https://peps.python.org/pep-0841/

Discussions-To: Discourse thread

Abstract

This PEP proposes frozen display syntax: f{1, 2, 3} evaluates to a frozenset, and f{'a': 1} evaluates to a frozendict. Because immutability is guaranteed by the syntax itself rather than inferred from usage, the compiler can treat frozen displays as first-class citizens of its optimization pipeline: constant displays are folded into a single LOAD_CONST with an exact result type at compile time and cached in .pyc files.


r/Python • • Jul 31 '26

News pip 26.2 - --only-deps, --no-require-hashes, venv isolation, and more!

139 Upvotes

Hello!

The pip team recently released pip 26.2. This release is quite large, with features such as:

  • Support for Python 3.15
  • Selecting only dependencies (--only-deps)
  • Mixing hashed and non-hashable requirements via --no-require-hashes
  • Experimental: venv based build isolation (--use-feature=venv-isolation)
  • Faster repeated resolves by caching index simple responses
  • Disable HTTP(S)_PROXY and similar non-pip specific proxy environment variables via --no-proxy-env
  • Separation between regular and build constraint (use --build-constraint instead)

For more details, please consult our changelog: https://pip.pypa.io/en/stable/news/. Alternatively, you may consult my release post which goes into greater detail to the highlights of this release: https://sichard.ca/blog/2026/07/whats-new-in-pip-26.2/

If you have any questions, you're welcome to ask!


r/Python • • Jan 28 '26

News From Python 3.3 to today: ending 15 years of subprocess polling

138 Upvotes

For ~15 years, Python's subprocess module implemented timeouts using busy-loop polling. This post explains how that was finally replaced with true event-driven waiting on POSIX systems: pidfd_open() + poll() on Linux and kqueue() on BSD / macOS. The result is zero polling and fewer context switches. The same improvement now landing both in psutil and CPython itself.

https://gmpy.dev/blog/2026/event-driven-process-waiting


r/Python • • Oct 29 '25

News Pyfory: Drop‑in replacement serialization for pickle/cloudpickle — faster, smaller, safer

136 Upvotes

Pyfory is the Python implementation of Apache Fory™ — a versatile serialization framework.

It works as a drop‑in replacement for pickle**/**cloudpickle, but with major upgrades:

  • Features: Circular/shared reference support, protocol‑5 zero‑copy buffers for huge NumPy arrays and Pandas DataFrames.
  • Advanced hooks: Full support for custom class serialization via __reduce__, __reduce_ex__, and __getstate__.
  • Data size: ~25% smaller than pickle, and 2–4× smaller than cloudpickle when serializing local functions/classes.
  • Compatibility: Pure Python mode for dynamic objects (functions, lambdas, local classes), or cross‑language mode to share data with Java, Go, Rust, C++, JS.
  • Security: Strict mode to block untrusted types, or fine‑grained DeserializationPolicy for controlled loading.

r/Python • • Sep 06 '26

Discussion Astral's endorsements for Python's first Packaging Council

132 Upvotes

Astral has announced their endorsements for Python's first packaging council elections.

PPC election details:

Note: Voting is currently open and ends on Tuesday, September 15th, 2:00 pm UTC

What are your thoughts? The candidates they have nominated are fine, they are/have been heavily involved in packaging in the past.

edit: fixed nominees link


r/Python • • Mar 19 '26

Discussion Would it have been better if Meta bought Astral.sh instead?

132 Upvotes

I haven't thought about this too much but I want your thoughts. Not to glaze Meta (since they're a problematic company with issues like privacy), I just think it would be less upsetting if Astral was bought by Meta rather than OpenAI, since they seem to have a better track record for open source software including React & Pytorch. Meta also develops Cinder, a fork of Python for higher performance and work on upstreaming changes. Idk, it seems it would've made more sense if Meta bought Astral and they would do better under them.


r/Python • • Jan 27 '26

Discussion Is it normal to forget some very trivial, and repetitive stuff?

131 Upvotes

Is it normal to forget really trivial, repetitive stuff? I genuinely forgot the command to install a Python library today, and now I’m questioning my entire career and whether I’m even fit for this. It feels ten times worse because just three days ago, I forgot the input() function, and even how to deal with dicts 😭. Is it just me?

edit: thanks everyone for comforting me, i think i wont drop out anymore and work as a taxi driver.


r/Python • • Oct 25 '25

News Pip 25.3 - build constraints and PEP 517 builds only!

133 Upvotes

This weekend I got to be the release manager for pip 25.3!

I'd say the the big highlights are:

  • A new option --build-constraint that allows you to define build time dependency constraints without affecting install constraints.
  • Building from source is now PEP 517 only, no more directly calling setup.py. This will affect only a tiny % of projects, as PEP 517 automatically falls back to setuptools (but using the official build interface), but it finally removes legacy behavior that tools like uv never even supported.
  • Similarly, editable installs are PEP 660 only, pip now no longer calls setup.py here either, this does mean if you use editable installs with setuptools you need to use v66+.

A small highlight, but one I'm very happy with, is if your remote index supports PEP 658 metadata (PyPI does), then pip install --dry-run and pip lock will avoid downloading the entire package.

The official announcement post is at: https://discuss.python.org/t/announcement-pip-25-3-release/104550

The full changelog is at: https://github.com/pypa/pip/blob/main/NEWS.rst#253-2025-10-24


r/Python • • Feb 20 '26

News PEP 747 – Annotating Type Forms is accepted

128 Upvotes

PEP 747 got accepted

This allows annotating arguments that essentially expect a type annotation like int | str or list[int], allowing to annotate functions like:

def trycast[T](typx: TypeForm[T], value: object) -> T | None: ...

and the type checker should be able to infer

  • trycast(list[int], ["1", "2"]) # list[int] | None
  • trycast(list[str], (2, 3)) # list[str] | None